williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 75 forks source link

getting error "result is not iterable" while iterating result of ".find() of mocked data #138

Open sagargadekar601 opened 3 years ago

sagargadekar601 commented 3 years ago

I am using document db with node.js and working on unit tests the "get" request. I have mocked the data using mongo-mock (version 4.0.0). Added a jest unit test for the "get" request in node.js.

I could mock the data db data correctly (referred the demo example of npm mongo-mock) but while fetching the data with .find() it is giving an error which says "result is not iterable". This error doesn't occur in real time scenario, only happening in unit tests. Below is the code :

const result = await dbData.find(filter); for await (let item of result) {

In result I see the correct data in count variable however it throws exception at "for" statement. I have mocked the data like below:

MongoClient.connect(url, {}, function(err, client) { var db = client.db(""mydb); // Get the documents collection var collection = db.collection("myCollection"); // Insert some documents var docs = "{"_id": "dbCollectionRecId", "testtId": "testId"}"; //collection.insertOne(docs); //insertMany collection.insertOne(docs, function(err, result) { console.log('inserted',result); } }

Let me know if I am missing anything.

sagargadekar601 commented 3 years ago

Can someone comment on this please?

shaunburdick commented 3 years ago

@sagargadekar601 Please understand that (like most open source libraries) this is written by volunteers on their free time. Issues are meant to raise possible problems with the library, not to demand support in your individual use case. I was planning on taking a look this weekend when I have some free time, but seeing as you are new and under an apparent time crunch I'll point you in the right direction to see if you can help yourself.

First, please take a look at Github's Markdown page. This should help you post your code snippets in a format that people can readily parse.

Second, the libraries code is open for you to view, so take a look! I would start by looking at the find() function here. You can see that the function returns a Cursor object (see line 125). You can take a look at the Cursor object here. From there you should see that it is iterable via the next() function. It also provides other functions it iterate such as each(), map(), forEach(), etc... My last hint is for you to take a look at the MDN on for await...of loops. There is a key point early in that article that you need to pay attention to.

Good luck @sagargadekar601 and please take my advice to heart. I hope one day you'll create or contribute to an open source library for the developer community!