首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >User.findOne()不是函数

User.findOne()不是函数
EN

Stack Overflow用户
提问于 2017-01-29 19:21:30
回答 13查看 46.5K关注 0票数 9
代码语言:javascript
运行
复制
passport.use('local-signup', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
(req, email, password, done) => {
    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(() => {

    // find a user whose email is the same as the forms email
    // we are checking to see if the user trying to login already exists
    User.findOne({ 'email' :  email },function(err, user){
        // if there are any errors, return the error

        if (err)
            return done(err);

        // check to see if theres already a user with that email
        if (user) {
            return done(null, false, {'errorMessages': 'That email is already taken.'});
        } else {

            // if there is no user with that email
            // create the user
            let newUser            = new User();

            // set the user's local credentials
            newUser.name       = req.body.fullname;
            //newUser.email          = email;
            newUser.password       = newUser.generateHash(password);


            // save the user
            newUser.save((err)=>{
                if (err)
                    return done(err);
                return done(null, newUser);
            });
        }
    });    
    });
}));

以上代码位于使用护照js身份验证的节点js中,并且本地注册代码不起作用。

在上面的代码中,我得到了错误:

User.findOne() is not a function

我的模式没问题..。请帮帮忙

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2017-01-29 19:38:51

您需要(如果您还没有)使用类似的model创建数据实例

代码语言:javascript
运行
复制
var UserDetails = mongoose.model('userInfo', UserDetail);

现在您应该可以在这里使用.findOne了。

并确保您在集合中为您的约会对象定义了结构,如..。

代码语言:javascript
运行
复制
 var Schema = mongoose.Schema;
 var UserDetail = new Schema({
  username: String,
  password: String
}, {
  collection: 'userInfo'
});
票数 12
EN

Stack Overflow用户

发布于 2019-04-01 14:48:41

请使用下面的代码

代码语言:javascript
运行
复制
module.exports = User = mongoose.model('user', UserSchema)

用户应该是模型名,并记住在顶部定义const UserSchema = new Schema,以便在MongoDB和

用户应该选择您拥有

代码语言:javascript
运行
复制
router.post('/user') (req, res) => { code here }

这样,您就可以将mongoose模式导出到路由用户,这使findOne可以被看作是mongoose函数。

票数 5
EN

Stack Overflow用户

发布于 2019-02-07 06:27:14

可能您没有从用户模型文件夹导出模型。例: module.exports =mongoose.model(“用户”,UserSchema);

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41924961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档