The official syntax for callback is:
user.findOne({username: "John"}).then((err,doc)=>{}
But this isnt working for me.
Whats working for me is this:
user.findOne({username: "John"}).then((doc,err)=>{}
Any suggestions why is this?
A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback.
A query also has a .then() function, and thus can be used as a promise.
1- Here pass error first
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
// Prints "Space Ghost is a talk show host".
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation);
});
2- Here pass document first
Band.findOne({name: "Guns N' Roses"}).then(function(doc) { // <- this is the Promise interface.
// use doc
});
Source : Mongoose documentation
Related
I would like to filter an array based on a mongoose query. However, I am relatively new to node.js and asynchronous programming. I am aware that Array.prototype.filter is a synchronous function and the mongoose queries are asynchronous.
When searching on this site I came across the following two solutions:
filtering an array with a function that returns a promise - but this looks quite complicated and I don't understand the concept yet.
how to wait for the result of a mongoose query? - at first this solution looks quite understandable, so I gave it a try
here is my example code based on the second approach:
exports.create = function(req, res) {
async.filter(req.body.data.entries, function(item, callback){
MyCollection.findOne({'_id': item.id}, function(err, doc) {
callback(err == null && doc != null);
});
},
function(results){
req.body.data = results
// default controller to create a document in Mongo DB
return controller.create(Model, req, res);
});
};
However, this does not seem to work. results does not correspond to a filtered list as described in the answer, but corresponds exactly to the result of the Boolean expression of the callback function. I also checked this in the current async documentation to see if anything has changed in the filter function, but didn't see any change.
Well, There is another way to do it. Instead of getting rows in async. You can get all the records filtered from database. There is a concept of $in in mongodb. You can use this to retrieve multiple records at a time with specific Ids. In your case, here is the exmaple
exports.create = function(req, res) {
var ids = []
req.body.data.entries.forEach(function (item) {
ids.push(item.id);
});
MyCollection.findOne({'_id': {$in: ids}}, function (err, docs) {
// here you have all your filter data
var myDocs = docs;
//Callback from here to return data
});
}
I am wondering how I can make two successives or synhcronous access to two different collections in a MongoDB database. I need to take a parameter in a first collection to use it as a parameter of .find() method in the second query. Here is my code:
MongoClient.connect(url, function(err, db) {
db.collection('questions').find( { "status": "active" } ).toArray(
function(err, item) {
var fb_id = item[0]._id;
console.log(fb_id);
db.close();
MongoClient.connect(url, function(err, db) {
var cursorC = db.collection('comments').find({questionId : fb_id }).toArray(
function(err, items) {
console.log(items);
}
);
db.close();
});
});
});
I tried unsuccessfully to chain the two connection to the database, however the second query states an undefined result.
When using both db.collection().find() function at the saeme level, in the MongoClient.connect() function I suppose both are executed asynchronously and it ends with another undefined result for the second function whose result depends on the first.
Do you have any idea to proceed using the MongoDB NodeJS driver I am using?
Thanks
I connect to a database and receive a client.
The next step is to create a collection (table).
db.createCollection("test", function(err, collection){ //I also tried db.collection(...
if(collection!=null)
{
collection.insert({"test":"value"}, function(error, model){console.log(model)});
}
else if(err!=null)
console.log(err);
});
Now I would have created a collection "test" as well as a document(row) "test" in it.
Next is to get the content of the collection:
db.test.find({}); //An empty query document ({}) selects all documents in the collection
Here I get the error: Cannot call "find" of undefined . So, what did I do wrong?
Edit: I connect to the database this way:
var mongoClient = new MongoClient(new Server("localhost", 27017, {native_parser:true}));
mongoClient.open(function(err,mongoclient){
if(mongoclient!=null)
{
var db = mongoclient.db("box_tests");
startServer(db);
}
else if(err!=null)
console.log(err);
});
In the mongo command line you can use db.test.find({}) but in javascript there is no way to replicate that interface so far (maybe with harmonies proxies some day).
So it throws an error Cannot call "find" of undefined because there is no test in db.
The api for the node.js driver of mongodb is like this:
db.collection('test').find({}).toArray(function (err, docs) {
//you have all the docs here.
});
Another complete example:
//this how you get a reference to the collection object:
var testColl = db.collection('test');
testColl.insert({ foo: 'bar' }, function (err, inserted) {
//the document is inserted at this point.
//Let's try to query
testColl.find({}).toArray(function (err, docs) {
//you have all the docs in the collection at this point
});
});
Also remember that mongodb is schema-less and you don't need to create the collections ahead of time. There are few specific cases like creating a capped collection and few others.
If you call db.test.find "next" after the db.createCollection block it ends up being immediately next before db.createCollection succeeds. So at that point, db.test is undefined.
Remember that node is async.
To get the results I believe you are expecting, db.test.find would have to be in the collection.insert callback where you're calling console.log(model).
db.createCollection("test", function(err, collection){
if(collection!=null)
{
// only at this point does db.test exist
collection.insert({"test":"value"}, function(error, model){
console.log(model)
// collection and inserted data available here
});
}
else if(err!=null)
console.log(err);
});
// code here executes immediately after you call createCollection but before it finishes
Checkout the node async.js module. Good writeup here: http://www.sebastianseilund.com/nodejs-async-in-practice
I have the following code in my User.js class:
exports.findUser = function(nameIn){
User.find({
name: nameIn
});
)};
How can I make it 'return' the users it found? Something is telling me that using "return" is not best practice on Node? I think I need to use callbacks but, how?
this is the code I am using to call the function:
var User = require("../models/user.js");
User.findUser(req.params.name, function(users) {
console.log(users);
});
In findUser you don't seem to be providing a place for that callback to go. The mongoose query itself also returns the document(s or error) to a callback to be handled.
If you wanted to modify your findUser to fit with how you seem to be using it:
exports.findUser = function(nameIn,callback){
User.find({name: nameIn}, function(err,user) {
if (err)
throw err;
callback(user)
});
)};
The results of mongoose's query (successful or not) are handed to the function in the arguments of the query (as a callback), which is in line with the asynchronous nature of Node. The other generally accepted methods of handling Node's non-blocking I/O are events and streams, with methods like promises being viable if somewhat controversial.
Mongoose itself uses callbacks in its guides, but also has promises available.
Looking at your function now, you're handing it the name to query, and what you'd like to do with the returned documents as a callback. The mongoose function itself already has the capability of doing everything:
User.find({name: req.params.name}, function(err,user) {
if (err)
throw err;
console.log(user);
});
Defining findUser may only be worthwhile if you have a lot of things to play with in that document that findUser will keep DRY.
I have the below schema (apologies that it is in coffeescript)
Schema = mongoose.Schema
AuthS = new Schema
auth: {type: String, unique: true}
nick: String
time: Date
Auth = mongoose.model 'Auth', AuthS
I simply want to recover one record which is definitely in my database:
Auth.findOne({nick: 'noname'}, function(obj) { console.log(obj); });
Unfortunately this always logs null. db.auths.findOne({nick: 'noname'}) in mongo shell always returns a value. What is going on?
Found the problem, need to use function(err,obj) instead:
Auth.findOne({nick: 'noname'}, function(err,obj) { console.log(obj); });
Mongoose basically wraps mongodb's api to give you a pseudo relational db api so queries are not going to be exactly like mongodb queries. Mongoose findOne query returns a query object, not a document. You can either use a callback as the solution suggests or as of v4+ findOne returns a thenable so you can use .then or await/async to retrieve the document.
// thenables
Auth.findOne({nick: 'noname'}).then(err, result) {console.log(result)};
Auth.findOne({nick: 'noname'}).then(function (doc) {console.log(doc)});
// To use a full fledge promise you will need to use .exec()
var auth = Auth.findOne({nick: 'noname'}).exec();
auth.then(function (doc) {console.log(doc)});
// async/await
async function async auth() {
const doc = await Auth.findOne({nick: 'noname'}).exec();
return doc;
}
auth();
See the docs if you would like to use a third party promise library.
In my case same error is there , I am using Asyanc / Await functions , for this needs to add AWAIT for findOne
Ex:const foundUser = User.findOne ({ "email" : req.body.email });
above , foundUser always contains Object value in both cases either user found or not because it's returning values before finishing findOne .
const foundUser = await User.findOne ({ "email" : req.body.email });
above , foundUser returns null if user is not there in collection with provided condition . If user found returns user document.
You might want to consider using console.log with the built-in "arguments" object:
console.log(arguments); // would have shown you [0] null, [1] yourResult
This will always output all of your arguments, no matter how many arguments you have.
Use obj[0].nick and you will get desired result,