1

I was trying to do a simple function with Node.js and Mongoose that returns true if the model is empty.

The mongoose configuration is fine:

var mongoose = require('mongoose');
var db = mongoose.createConnection( 'mongodb://localhost:27017/prueba' );

var userSchema = mongoose.Schema({
    phoneNumber: Number,
    name: String
});
var User = db.model('User', userSchema, 'User'');

Then I tried to do this:

User.find(function(err, data) {
    if (err) {console.log(err)};
    console.log(data.length == 0 );
});

And it works fine, it logs true, or false.

Then I tried to do:

var isUsersEmpty =  function () {
    User.find(function(err, data) {
        if (err) {console.log(err)};
        console.log(data.length == 0);
    });
}
isUsersEmpty();

And again it works fine, it logs true or false, buy if I do:

var isUsersEmpty2 = function () {
    User.find(function(err, data) {
        if (err) {console.log(err)};
        return data.length == 1;
    });
}
console.log(isUsersEmpty2());

Then the log prints "Undefined". What can I do if I need a function that returns true or false to do things like this:

if (isUsersEmpty2()) {} //Do something here... 

And isUsersEmpty2() returns always undefined.

1

1 Answer 1

1

isUsersEmpty2() returns a promise , which means you can't just log it like you did. You need to send a response from the function. This should work:

var isUsersEmpty2 = function (res) {
User.find(function(err, data) {
    if (err) res(err, null);
    res(null, data.length == 1);
});
}

isUsersEmpty2(function(err, res) {
   if(res) {/*do something*/}
});
5
  • If I need a function that returns true or false, not a console log. To use it in conditionals. Something like: if(isUsersEmpty2) {dosomethinghere}.
    – Enrique
    Commented Jun 29, 2015 at 15:41
  • res will have the value true or false; I edited my answer
    – Markus
    Commented Jun 29, 2015 at 15:44
  • I can't understand well how does it works, I will try to explain you what I wanna do with all the details. I am doing a function that, if the database is empty, it will save some data on it, and if the database already has data, the function will do something with the data. I need to call the function that returns "true" or "false" in a external conditional. The code looks like:
    – Enrique
    Commented Jun 29, 2015 at 16:02
  • var isModelEmpty = function(){ Model.find(function(err, data){ //Should return true if data.length == 0 })} Now I can use this function to know if the model is empty. if(isModelEmpty()) { // Save some new data }else { // Do something whit the data }
    – Enrique
    Commented Jun 29, 2015 at 16:02
  • the code you wrote will only work in a synchronous way, but since in node pretty much everything is async you need to call a function and wait for the callback (done in the example with res(null, data.length==1). After the callback you can easily do what you wanted, just check the variable res
    – Markus
    Commented Jun 29, 2015 at 16:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.