I use nw.js + nedb. How to output the value of docs in the global variable resultVal. Now resulVal does not display anything.

var Datastore = require('nedb'); var db = new Datastore({filename : 'users'}); db.loadDatabase(); var resultVal = ""; db.find({}, function (err, docs){ console.log (docs); resultVal = docs; }); console.log (resultVal); 

1 answer 1

Function,

 function (err, docs){ console.log (docs); resultVal = docs; } 

which is passed as the second parameter to the find method is a callback (or in other words, a "callback function"). They are widely used in JavaScript, for example, in asynchronous code, in event handlers, in the setTimeout and setInterval methods.

But, if done like this, it will output:

 var Datastore = require('nedb'); var db = new Datastore({filename : 'users'}); db.loadDatabase(); var resultVal = ""; db.find({}, function (err, docs){ console.log (docs); resultVal = docs; console.log (resultVal); });