我正在开发一个Node服务器应用程序,它可以向这个端点注册新用户:
// '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向其发送电子邮件,但我将返回以下错误:
[2019-08-19T13:30:46.148] [ERROR] default - /register - Error: -2 Expected a `length` to be a non-negative finite number我不明白为什么!该帐户已在mongo DB中正确创建!
post参数示例:
{
"email": "myemail@gmail.com", "nation": "US", "password": "apassword"
}这里是帐户模型
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);非常感谢您的帮助!
发布于 2019-08-19 19:45:55
这是这行上的一个Typo:
passport.authenticate('local', { session: false })(req, res, (err) => {试着这样做:
passport.authenticate('local', { session: false } ,(err, user, info) => {https://stackoverflow.com/questions/57555950
复制相似问题