This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have the following code to query a MongoDB database:
var docs;
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
findDocuments(db, function() {
console.log(docs);
client.close();
});
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
//console.log(docs);
callback(docs);
return docs;
});
};
}
Which outputs:
Connected successfully to server
Found the following records
undefined
I want to use the results of the query which are stored inthe variable docs for further processing. However they donĀ“t get returned from the function. i.e. the the expression
findDocuments(db, function() {
console.log(docs);
client.close();
});
I get an "undefined" returned. What am I doing wrong?
You need to update the findDocuments function call as follows,
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
You don't need the docs variable at the top. Use local variables as follows,
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
return callback(docs);
});
}
Also note that I removed the return docs statement, as it doesn't have any importance along with the callback.
Finally, I suggest you to learn more about callbacks (and preferably promises)
Change this
function() {
console.log(docs);
client.close();
});
to this
function(docs) {
console.log(docs);
client.close();
});
because in your code you log docs variable on the top of your code which hadn't receive any value try the new code and tell me . does it work now ? I think yes .
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I am making a website where i need to import data from database and use it later in program. But when i am using npm package mysql i can't find any way to import result of query outside function for later use. Here is an example of my code.
Function with that i get data from database:
let output;
dba.query('SELECT * FROM `movies-web`.movies;',(err, rows) => {
if(err) {
throw err;
} else {
output = rows;
}
});
And in code i use it like this:
res.write(output);
But the variable output return undefined.
I am solving this problem for longer than month, i browsed so many pages, questions here on stackoverflow and i can't find working solution.
You can't write code like that, I suggest you read about callback documents and learn how to use asynchronous in the node is
https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/
https://www.w3resource.com/node.js/nodejs-programming-model.php
https://stackoverflow.com/a/19739852/6759368
Your code should write like that:
dba.query('SELECT * FROM `movies-web`.movies;',(err, rows) => {
if(err) {
throw err;
} else {
res.write(rows)
}
});
P.S:
A better scenario for use callback:
const http = require('http');
function executeQuery(next) {
dba.query('SELECT * FROM `movies-web`.movies;',(err, rows) => {
if(err) {
console.error(err);
next(err);
return
}
next(null, rows);
});
}
const server = http.createServer(function (req, res) {
executeQuery(function (error, output) {
if (error) {
res.writeHead(400);
res.end(error.message);
return;
}
res.writeHead(200);
res.end(JSON.stringify(output));
});
});
server.listen(8080);
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I'm creating a Node project cause i want to knwo how to use mongoose but i'm facing a problem right now. I'm getting an undefined variable while returing a collection from my MongoDB database. This is my code:
app.js
...
case 'index-user':
let indexUserResponse = userController.index();
console.log(indexUserResponse); // undefined
break;
...
user.js (controller)
...
const index = () => {
User.find({}, function(err, res) {
if (err) throw err;
return res;
});
}
...
I know that there is a user on the database and i can see it if add a console.log(user) just before the return res; so... What is happening here? I don't get it.
The issue is that userController.index() is an calling an async function.
You should use a callback or a promise in order to fetch the data from you db.
Promise example:
Here we are returning a promise from the the executing the query on the Users collection.
const index = () => User.find({}).exec();
And in app.js:
case 'index-user':
userController.index().then(result => {
console.log(result); // as you use find there will be a list of user documents
});
break;
Or, you can use good old callbacks as so:
const index = (callback) => {
User.find({}, function(err, res) {
if (err) throw err;
return callback(res);
});
}
And in app.js:
case 'index-user':
let indexUserResponse = userController.index((result) => {
console.log(result);
});
break;
When you execute it the way you do now, the console.log() runs before the index() method can execute the find() callback.
Hope it's clear enough and helps
While using sails 1.0 I run into a problem.
The sails documentation describes that the create function's callback (or promise) has an argument describing the inserted record. In my case (see code below) this value is always "undefined". I can verify that the entry is however added to the database. (Both using postgresql and the localdiskdb). - So the model is set up correctly.
The route connects (correctly) to:
add: function (req, res) {
const params = req.allParams();
const {
event_id,
name,
date,
} = params;
const dat = {
name:name,
id:Number(event_id),
date_start:new Date(Number(date)),
};
console.log(dat);
console.log(typeof dat.location);
Event.create(dat)
.then(function (result){
console.log(result)
return res.ok();
})
.catch(function (err){
console.log(err);
return res.send(err);
});
}
So why is the result in my callback undefined?
With Sails.js v1, you will have to include the .fetch() function to send back records that were updated/destroyed/created when performing an .update(), .create(), .createEach() or .destroy() query. Otherwise, no data will be returned (or if you are using callbacks, the second argument to the .exec() callback will be undefined.)
The official example:
var newUser = await User.create({ fullName: 'Alice McBailey' }).fetch();
For your code above, try:
Event.create(dat).fetch().exec(function(err, result){
if(err) {
console.log(err);
return res.send(err);
}
console.log(result)
return res.ok();
})
For more info, see the official sails documentation on .fetch() here.
I'm writing an node.js API with Mongoose,
But for the purpose of a task, I want to get the object as a variable from my find,
so I have this :
exports.get_info = function(_id) {
Session.findById(_id, function(err, session) {
if (err)
res.send(err);
console.log (session); // the object is good
return session; // trying to return it
});
};
but When I call :
session_ = sessions.get_info(id);
here session_ is undefined ...
Any ideas ?
the right way to do it is :
exports.get_info = function(_id,callback) {
Session.findById(_id, function(err, session) {
if (err)
res.send(err);
console.log (session); // the object is good
callback(null,session); // trying to return it
});
};
in this way, you can get the session like this :
sessions.get_info(id,function(err,session){
if(err) console.log(err);
console.log(session);
});
Mongoose model find it's an sync function, you can't get result immediately. You should use callback or much better solution - promise (mongoose supports promises):
exports.get_info = function(_id) {
return Session.findById(_id);
};
get_info(/* object id */)
.then(session > console.log(session))
.catch(err => console.log(err));
Ive been playing around with mongodb in node.js. I have made a basic collection with some data (i know its there ive checked). When I try to run a find() on the collection it returns undefined. I dont know why this is. The code is below:
function get_accounts(){
var MongoClient = mongodb.MongoClient;
var url = "url";
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to database');
var collection = db.collection('accounts');
collection.find().toArray(function(err, docs) {
console.log("Printing docs from Array")
docs.forEach(function(doc) {
console.log("Doc from Array ");
console.dir(doc);
});
});
console.log("mission complete");
}
db.close();
}
);
}
If you know why this is happening i would like to hear your thoughts. thanks! The database is a mongolab hosted database if that makes any difference.
You are getting an undefined value because of the asynchronous nature of node.js, nowhere in your code exists logic that tells the console.log statement to wait until the find() statement finishes before it prints out the documents. You have to understand the concept of callbacks in Node.js. There are a few problems here, though, that you could fix. A lot of people getting started with node have the tendency to nest lots of anonymous functions, creating the dreaded "pyramid of doom" or callback hell. By breaking out some functions and naming them, you can make it a lot cleaner and easier to follow:
var MongoClient = require("mongodb").MongoClient
// move connecting to mongo logic into a function to avoid the "pyramid of doom"
function getConnection(cb) {
MongoClient.connect("your-mongo-url", function(err, db) {
if (err) return cb(err);
var accounts = db.collection("accounts");
cb(null, accounts);
})
}
// list all of the documents by passing an empty selector.
// This returns a 'cursor' which allows you to walk through the documents
function readAll(collection, cb) {
collection.find({}, cb);
}
function printAccount(account) {
// make sure you found your account!
if (!account) {
console.log("Couldn't find the account you asked for!");
}
console.log("Account from Array "+ account);
}
// the each method allows you to walk through the result set,
// notice the callback, as every time the callback
// is called, there is another chance of an error
function printAccounts(accounts, cb) {
accounts.each(function(err, account) {
if (err) return cb(err);
printAccount(account);
});
}
function get_accounts(cb) {
getConnection(function(err, collection) {
if (err) return cb(err);
// need to make sure to close the database, otherwise the process
// won't stop
function processAccounts(err, accounts) {
if (err) return cb(err);
// the callback to each is called for every result,
// once it returns a null, you know
// the result set is done
accounts.each(function(err, account) {
if (err) return cb(err)
if (hero) {
printAccount(account);
} else {
collection.db.close();
cb();
}
})
}
readAll(collection, processAccounts);
})
}
// Call the get_accounts function
get_accounts(function(err) {
if (err) {
console.log("had an error!", err);
process.exit(1);
}
});
You might have to add an empty JSON object inside the find.
collection.find({})
Documentation can be found here.
You must enter this code in an async function and you will be fine here data is the your desired value and you must use promises to not make your code look messy.
var accountCollection = db.collection('accounts);
let data = await accountCollection.find().toArray.then(data=>data).catch(err=>err);