Retrieving mongodb data from javascript function


#1

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.


#2

what’s the error you’re getting?


#3

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.


#4

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


#5

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


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

#7

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


#8

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()

#9

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