I have a function using mongoose to query my database. The connection already works. However, if I do not use actual strings, but instead string variables, the .find returns an undefined object (i.e. it doesn't find anything). Here is the code:
function getUser(obj, callback){
console.log("obj.key: " + obj.key[0].toString() + ":" + obj.value[0].toString());
var first = "name.first";
console.log("first: " + first);
var second = "nicholas";
console.log("second: " + second);
User.findOne({first:second}, 'name telephone address1 address2 city state zip country', function(err, user){//nothing can be found when not directly using a string!!!
console.log("got this user: " + user.name);
});
}
this does not work, however, if I replace the line with .find with this, it does:
User.findOne({"name.first":"nicholas"}, 'name telephone address1 address2 city state zip country', function(err, user){
I've never seen anything like this before. Normally a string is a string and it will work no matter what you do with it. Any ideas what might be wrong?
p.s.the console.logs:
obj.key: name.first:nicholas
first: name.first
second: nicholas
error at: console.log("got this user...cannot read property 'name' of null.