Echo
Echo

Reputation: 305

Node js / Mongoose .find()

Im having trouble using the .find() function within mongoose on a node js server I've been trying to use this but I cannot get the key information out of my database.

user.find({key: 1} , function(err, data){
  if(err){
    console.log(err);
  };
  console.log("should be the key VVV");
  console.log(data.key);
});

I'm mainly just having trouble wrapping my head around how this function takes queries and gives you back the response from your DB. If someone can break it down id be very thankful the mongoose docs weren't much help.

Also this is my user schema if it helps

var userSchema = new mongoose.Schema({
  username: {type: String, unique: true},
  password: {type: String},
  key: {type: String},
  keySecret: {type: String}
}, {collection: 'user'});


var User = mongoose.model('user',userSchema);

module.exports = User;

Upvotes: 7

Views: 31526

Answers (3)

bahri noredine
bahri noredine

Reputation: 751

hi my friend I use this method to retrieve data from db I hope that can help

user.find({key: 1})
   .then((userOne)=>{
       //if is an API
       res.status(200).json({data : userOne});
       //OR
       // if you had a view named profile.ejs for example
       res.render('/profile',{data : userOne, title : 'User profile' }); 
            console.log(userOne); // just for check in console
        })
    .catch((error)=>{
        //if an api
        res.status(400).json({messageError : error});
        // OR if not an api
        res.render('/profile',{data : 'No data of this profile',errorToDisplay : error})
    });
});

Upvotes: 0

lutaoact
lutaoact

Reputation: 4429

Maybe you should use

Model.findOne({key: '1'}, function(err, data) {
  console.log(data.key);
});

find() will get a doc array, and findOne() can get just one doc.

Your field key is String type, so your query obj shoule be {key: '1'}, not {key: 1}.

Read the mongoose docs carefully may help you.

Upvotes: 4

David Alsh
David Alsh

Reputation: 7677

If you imagine your DB looking like this:

[
    {
        "name": "Jess",
        "location": "Auckland"
    },
    {
        "name": "Dave",
        "location": "Sydney"
    },
    {
        "name": "Pete",
        "location": "Brisbane"
    },
    {
        "name": "Justin",
        "location": "Auckland"
    },
]

executing the following query;

myDB.find({location: 'Brisbane'})

will return:

[
    {
        "name": "Pete",
        "location": "Brisbane"
    }
]

While myDB.find({location: 'Auckland'}) will give you

[
    {
        "name": "Jess",
        "location": "Auckland"
    },
    {
        "name": "Justin",
        "location": "Auckland"
    },
]

As you can see, you're looking through the array for a key that matches the one you're looking to find and gives you back all of the documents that match that key search in the form of an array.

The Mongoose interface gives this data to you in the form of a callback, and you just need to look for the item inside of the array it returns

user.find({location: "Auckland"}, function(err, data){
    if(err){
        console.log(err);
        return
    }

    if(data.length == 0) {
        console.log("No record found")
        return
    }

    console.log(data[0].name);
})

Upvotes: 14

Related Questions