1

How to know what all methods in mongoDb has an inbuilt promise in it. eg: "updateOne() , findOne()ACCESSING INBUILT PROMISE OF "INSERTONE()" USING ".THEN"" these methods have inbuilt promises and and we can access the response using ".then" but for lots of other mongoDB methods lack this feature how can we be sure of which methods dont have inbuilt promise in them?

eg: "find()" has no inbuilt promise so we cannott perform "find().then((response)=>{})" this will give an error. Whereas "findOne().then((response)=>{})" will work without any issue.

  • This looks a lot like the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it). And you're not using the promise returned by the inner `insertOne` call – Bergi Nov 07 '22 at 07:30

3 Answers3

0

This is inconsistent across the NodeJS MongoDB driver as some methods return more complex objects for manipulating the returned values. For example, .find() returns a FindCursor object which can be used to iterate over results by calling .next() repeatedly.

I would recommend referring the MongoDB documentation for the NodeJS driver (found here, or common usage examples are here) rather frequently. The documentation is reasonably extensive and should help with issues like this.

You can also consider using TypeScript, which I have personally found helpful for cases such as this because you can easily tell what type of object is returned from a function/method call.

Dakeyras
  • 1,829
  • 5
  • 26
  • 33
0

I would recommend referring the MongoDB documentation for the NodeJS driver (found here, or common usage examples are here) rather frequently. The documentation is reasonably extensive and should help with issues like this

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 16:16
0

There is some inconsistency over the find method in MongoDB native driver in node Js. This is because the reason that the finds method returns a cursor. So what we could do here is convert that to an array using the toArray() method.

The best solution here would be to use async await over promise chaining. This would provide us with a cleaner and easier syntax to work with.

Eg : Let's say we want to find all the products in the product collection. Below is a function for doing exactly that.

const findAll=async(userId)=>{
    const userData= await db.products.find().toArray();
    console.log(userData);
    return userData;
}

Upon calling the above function we would get all the products in the product collection. Just by looking at the code, we could see that it provides a more readable syntax than chaining promises all over.

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Ravi_rs
  • 1
  • 2