首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >passport-local奇怪错误:“期望` `length` `为非负的有限数”

passport-local奇怪错误:“期望` `length` `为非负的有限数”
EN

Stack Overflow用户
提问于 2019-08-19 19:36:24
回答 1查看 180关注 0票数 0

我正在开发一个Node服务器应用程序,它可以向这个端点注册新用户:

代码语言:javascript
复制
// 'Create a new account
api.post('/register', (req, res) => {
    try {

        // Create new user and register it with passport
        let newUser = new AccountModel({ 
            username: req.body.email,

            verificationToken: "",

            active: false,
            displayName: "Uknown",
            profileImageUrl: "",
            profileImagePubid: "",
            likes: [],
            nation: req.body.nation,
            points: 0
        });

        AccountModel.register(newUser, req.body.password, function(err, account) {
            if(err){ // this check if the user name already exists.
                writeLog("ERROR", req.url + " - Error: -1 " + err.message);
                res.status(500).send( { error: err.message, errnum: -1 } );
                return;
            }

            passport.authenticate('local', { session: false })(req, res, (err)  => {
                if(err){
                    writeLog("ERROR",req.url + " - Error: -2 " + err.message);
                    res.status(500).send( { error: err.message, errnum: -2 } );
                    return; 
                }

                // Generate the verification token
                var strVer = crypto(16);

                // Update user information with the verification token
                account.verificationToken = strVer;
                account.active = false
                account.save( (err, account) => {
                    if(err) {
                        writeLog("ERROR",req.url + " - Error: -3 " + err.message);
                        res.status(500).send( { error: err.message, errnum: -3 } );
                        return;
                    }

                    // Send an email for account verification
                    var dataToSend = {
                        "user_email": account.username,
                        "verification_token": strVer
                    };
                    sendEmailSendgrid(account.username, process.env.SENDGRID_TEMPLATE_VER_EMAIL, "Confirm your email address please!", dataToSend);

                    writeLog("INFO",req.url + " - Successfully created new account - " + account.username);
                    res.status(200).send( { message : "Successfully created new account", token: strVer } );
                    return;
                });
            });
        });

    } catch (err) {
        writeLog("ERROR",req.url + " - Unexpected error registering the new account. " + err.message);
        res.status(500).send( { error: "Unexpected error registering the new account." + err.message } );
        return;
    }
});

这将创建新用户,然后通过sendgrid.com向其发送电子邮件,但我将返回以下错误:

代码语言:javascript
复制
[2019-08-19T13:30:46.148] [ERROR] default - /register - Error: -2 Expected a `length` to be a non-negative finite number

我不明白为什么!该帐户已在mongo DB中正确创建!

post参数示例:

代码语言:javascript
复制
{
"email": "myemail@gmail.com", "nation": "US", "password": "apassword"
}

这里是帐户模型

代码语言:javascript
复制
import mongoose from 'mongoose';
import passportLocalMongoose from 'passport-local-mongoose';
const Schema = mongoose.Schema;

let Account = new Schema ({
   active: { type: Boolean, default: false },
   displayName: { type: String, default: "Uknown" },
   verificationToken: { type: String, default: "" },
   profileImageUrl: { type: String, default: "" },
   profileImagePubid: { type: String, default: "" },
   likes: [{ type: Schema.Types.ObjectId, ref: "Spot" }],
   nation: { type: String, default: "ND" },
   points: { type: Number, default: 0 }
});

Account.plugin(passportLocalMongoose); // attach the passport-local-mongoose plugin
module.exports = mongoose.model('Account', Account);

非常感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2019-08-19 19:45:55

这是这行上的一个Typo:

代码语言:javascript
复制
    passport.authenticate('local', { session: false })(req, res, (err)  => {

试着这样做:

代码语言:javascript
复制
     passport.authenticate('local', { session: false } ,(err, user, info)  => {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57555950

复制
相关文章

相似问题

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