Bug in Querying mongoDB in getStaticProps ? #13457
Unanswered
oshanfernando
asked this question in
Help
Replies: 1 comment 4 replies
-
The thing that might be happening is that the code is executed client side. If there is an instance of MongoClient executed outside of |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Although the documentation states that we can write direct database queries in the getStaticProps method, it seems like querying the MongoDB from a field other than the _id does not work.
await client.db('blogdb').collection('posts').findOne({'urlname': params.meta}).toArray()
throws the the following error:But if I query from the id, it works
await client.db('blogdb').collection('posts').find(ObjectID(params.meta)).toArray()
If anyone came across this issue, please suggest a workaround for this, other than writing a separate backend api to fetch the data.
Beta Was this translation helpful? Give feedback.
All reactions