Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sagargadekar601 opened this issue Jan 19, 2021 · 2 comments · May be fixed by #146
Open

Comments

@sagargadekar601
Copy link

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
Copy link
Author

Can someone comment on this please?

@shaunburdick
Copy link
Collaborator

@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!

@PacoDu PacoDu linked a pull request Mar 3, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants