简体   繁体   中英

findOne not working in mongoose/node

This question is to do with the mongoose module for the NodeJS platform.

When I run the below, how do I get to the response from the DB?:

router.get('/profileSettings', securePages, function(req, res, next){
    databaseUserModel.findOne({'profileID':req.session.facebookProfileId}, function(err, userFromDB) {
        if(userFromDB){
            done(null, userFromDB);
        } else {
            console.log('Result does not exist');
       }
    });

    res.render('profileSettings', {title:siteName + ': Profile Settings', user:userFromDB});
})

It doesn't seem to work for me as userFromDB doesn't seem to exist - both in the findOne and also in the res.render methods! (undefined to be precise)

My databaseUserModel looks like this:

var databaseUser = new mongoose.Schema({
   profileID:String,
   fullname:String,
   profilePic:String,
   email:String,
   birthday:String,
   location:String,
   about:String
});

var databaseUserModel = mongoose.model('databaseUser', databaseUser);

The error message is:

ReferenceError: userFromDB is not defined
at Object.handle (/Users/shayan/Repos/imo/routes/routes.js:75:84)
at next_layer (/Users/shayan/Repos/imo/node_modules/express/lib/router/route.js:103:13)
at Object.securePages [as handle] (/Users/shayan/Repos/imo/routes/routes.js:20:13)
at next_layer (/Users/shayan/Repos/imo/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/Users/shayan/Repos/imo/node_modules/express/lib/router/route.js:107:5)
at /Users/shayan/Repos/imo/node_modules/express/lib/router/index.js:205:24
at Function.proto.process_params (/Users/shayan/Repos/imo/node_modules/express/lib/router/index.js:269:12)
at next (/Users/shayan/Repos/imo/node_modules/express/lib/router/index.js:199:19)
at next (/Users/shayan/Repos/imo/node_modules/express/lib/router/index.js:176:38)

I feel it maybe something very simple I may be missing but I am just getting my hands dirty on NodeJS (and actually developing as a whole). I was coding in a niche language for my company before (they have developed it) so please bear with me.

Any input will help guys! I am really stuck on this

PS: my mongoose object works as I have used it to store data.

You can use result data only inside mongoose query ie you cannot use value return from userFromDB outside of findOne . Put your res.render inside it will solve.

router.get('/profileSettings', securePages, function(req, res, next){
    databaseUserModel.findOne({'profileID':req.session.facebookProfileId}, function(err, userFromDB) {
        if(userFromDB){
            res.render('profileSettings', {title:siteName + ': Profile Settings', user:userFromDB});
        } else {
            console.log('Result does not exist');
       }
    });
})

Sometimes, we miss the await keyword before findOne which results in inappropriate results.

My Code before

let allUser = User.find({});

And all my business logic failed as I haven't retrieved it

My code after await

let allUser = await User.find({});

Now it retrieves the data correctly and my code works.

This is a mistake which I did and I thought findOne wasn't working. Hope it helps people to refrain from such mistakes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM