Get node.js neDB data into a variable

I can insert and retrieve data from neDB database in nodejs. But I cannot pass data outside the function that retrieves it.

I read the neDB documentation and I searched and tried different combinations of callbacks and returns (see code below) without finding a solution.

I am new to javascript, so I don’t know if I don’t understand how to use variables at all, or is this problem related to using neDB specifically or both.

Can someone explain why the "x" in my code never contains JSON docs results from the database? How can I make it work?

var fs = require('fs'), Datastore = require('nedb') , db = new Datastore({ filename: 'datastore', autoload: true }); //generate data to add to datafile var document = { Shift: "Late" , StartTime: "4:00PM" , EndTime: "12:00AM" }; // add the generated data to datafile db.insert(document, function (err, newDoc) { }); //test to ensure that this search returns data db.find({ }, function (err, docs) { console.log(JSON.stringify(docs)); // logs all of the data in docs }); //attempt to get a variable "x" that has all //of the data from the datafile var x = function(err, callback){ db.find({ }, function (err, docs) { callback(docs); }); }; console.log(x); //logs "[Function]" var x = db.find({ }, function (err, docs) { return docs; }); console.log(x); //logs "undefined" var x = db.find({ }, function (err, docs) { }); console.log(x); //logs "undefined"* 
+6
source share
2 answers

Callbacks are usually asynchronous in JavaScript, which means you cannot use the assignment operator, therefore, you are not returning anything from the callback function.

When you call the async function function executed by your program, passing the instruction "var x = whatever". Assigning a variable, the result of any callback, needs to be done from the callback itself ... what you need is something in the lines ...

 var x = null; db.find({ }, function (err, docs) { x = docs; do_something_when_you_get_your_result(); }); function do_something_when_you_get_your_result() { console.log(x); // x have docs now } 

EDIT

Here 's a good blog post on asynchronous programming. And there are many more resources on this subject that you can get.

This is a popular library that will help with asynchronous flow control for node.

PS
Hope this helps. Please be sure to ask if you need to clarify something :)

+6
source

I needed to learn a little about asynchronous functions so that everything was correct. For those who are looking for some help in getting the return value from nedb, here is a snippet of what worked for me. I used it in an electron.

 function findUser(searchParams,callBackFn) { db.find({}, function (err, docs)) { //executes the callback callBackFn(docs) }; } usage findUser('John Doe',/*this is the callback -> */function(users){ for(i = 0; i < users.length; i++){ //the data will be here now //eg users.phone will display the user phone number }}) 
0
source

Source: https://habr.com/ru/post/955469/


All Articles