简体   繁体   中英

Accessing schema properties from Mongoose methods

usermodel.js

var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String,
    }
});

userSchema.methods.validPassword = function(password) {
    user=this; 
    return this.local.password; <---------------THIS LINE
};

module.exports = mongoose.model('User', userSchema);

So I call something in server.js

server.js

var User = require('./usermodel.js);

var objectUser = {
    email: "a2@a.com",
    password: "a222"
};

console.log(objectUser.User.validPassword());

Can it return the actual email?

How can you access a property of a user from the database without using?

db.find({email: "a2@a.com"}).....

You can search through your database with an api call, so if I was trying to check a users email for a login post I would do something like this. This here is set up for a form request on any user trying to login to the application.

signIn: function (req, res) { var $email = req.body.email mongoDB.User.findOne({email: $email}, function(err, user){ if(err){res.json(err)} // Check if a User exists if(user){ // Do whatever you want to do with the users info. Typically you would do such things as verify passwords and what not. } }) }

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