Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get node.js neDB data into a variable

I am able to insert and retrieve data from an neDB database in nodejs. But I cannot pass the data outside of the function that retrieves it.

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

I'm new to javascript so I do not know if I am misunderstanding how to use variables in general or if this issue is related to using neDB specifically or both.

Could someone please explain why "x" in my code does not ever contain the docs JSON 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"*
like image 837
curtwphillips Avatar asked Oct 08 '13 06:10

curtwphillips


2 Answers

Callbacks are generally asynchronous in JavaScript meaning you can not use assignment operator, consequently you do not return anything from the callback function.

When you call an async function execution of you programme carries on, passing the 'var x = whatever' statement. The assignment to a variable, the result of whatever callback is received, you need to perform from within the callback itself... what you need is something in the lines of ...

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 is a nice blog post about asynchronous programming. And there is a lot more resources on this topic that you can pick up.

This is a popular library to help with async flow-control for node.

P.S.
Hope this helps. Please, by all means ask if you need something clarified :)

like image 139
Dmitry Matveev Avatar answered Nov 09 '22 01:11

Dmitry Matveev


I ran into the same problem. In the end I used a combination between async-await and a promise with resolve to solve it.

In your example the following would work:

var x = new Promise((resolve,reject) {
    db.find({ }, function (err, docs) {
        resolve(docs);
    });
});

console.log(x);
like image 5
Duitel Avatar answered Nov 09 '22 00:11

Duitel