Hi, I am a beginner and I am running a query that fetches all documents that equal an id value. Once, the cursor object is returned by MongoDB, I have to iterate over all the documents and build a string array object and then return it as a response. My code is as follows:var userID =req.body.userID const db = mongoUtil.getDb() var jsonObj= "" db.collection('devices').find({"user_id": ObjectID(userID)}, (err, devices) => { devices.forEach((device) => { jsonObj+= device.device_name + "," }) res.send(jsonObj.toString()) }) The output of the above code is just "". Can someone help me to modify the code so that it works in a synchronous manner?
Submitted July 21, 2019 at 04:35PM by sainatarajan7
No comments:
Post a Comment