node.js - Querying Mongo Database -
i need perform query on following entry in mongo database:
{ "_id" : objectid("597b19512a5b1c3258e6440e"), "fdata" : [ { "type" : "header", "subtype" : "h1", "label" : "date of commencement" }, { "type" : "paragraph", "subtype" : "p", "label" : "the partnership business shall deemed have commenced on , :" }, { "type" : "date", "label" : "date field", "description" : "enter correct date per instructions in clause", "classname" : "form-control", "name" : "date-1501239537753" }, { "type" : "button", "subtype" : "submit", "label" : "next", "classname" : "btn btn-primary", "name" : "button-1501239595350", "style" : "primary" } ] }
i have fetch entire array called fdata , parameter available me value of label in array called cl so:
cl = ['date of commencement'];
my question how access array entry following code gives me 'unexpected token'
my query this:(using nodejs in backend)
for(var i=0; i<cl.length ;i++) { var dummy = cl[i]; //console.log(dummy); result[i] = db.collection('clauses').findone({fdata[0].label: dummy},{fdata}); }
its in loop because in cases array cl have multiple entries.
the findone query you're using asynchronous. why see [ promise { <pending> } ]
. there lot of approaches handle asynchronous behavior:
- callbacks
- async library
- promises
- generators + coroutines
- async + await
if nodejs version new enough (8+) should support async/await approach.
async function dojob(arr) { const clauses = db.collection('clauses'); (const key of arr) { const result = await clauses.findone({'fdata.label': key}); console.log(result); } }
Comments
Post a Comment