Retrieving mongodb data from javascript function


I have difficulty assigning data query result from mongodb.find() in Javascipt query language.

This is the code…

function getData()
var vdata = mydata.find( { branch: “INA” } ) ;
document.getElementById(“txtBranch”).innerHTML = vdata.branch ;

This doesn’t work. Please give me an advice. Thanks.


what’s the error you’re getting?


the find funtion return a cursor, not data.
You have to use findOne to get you unique data.
Or fetch() on the cursor to get an array of results.


there’s no error, just txtbranch doesn’t get the value…


could you give me a sample… I have try looking the sample, including, cursor… etc… but still not get the result yet…

var vdata = mydata.find( { branch: "INA" } ).fetch()


It doesn’t work… when i try to show the variable value : alert( vdata[0] ) … it shows “undifened” message


try it in browser console to see if it should return something or so

this you should be able to do in console:

mydata.find( { branch: "INA" } ).fetch()


great. thanks for your advice. … I finally found the object method… it is alert( vdata[0].branch ));