我在server.js中定义了以下内容,
app.post('/register')
我有一个在发布到我的module.exports.registerUser = (userData) => {
return new Promise((resolve, reject) => {
if (userData.password != userData.password2) {
reject("Passwords do not match!");
}
let newUser = new User(userData);//<!-- 'Error: TypeError: User is not a constructor'
newUser.save((err) => {
if(err.code == 11000) {
reject("Username already taken");
} else {
reject("Error creating User: " + err);
}
// exit the program after saving
//process.exit();
resolve();
});
})
}
路由时调用的函数,它基本上构建一个新用户,然后将其分配给传递的数据,然后解析它。
Error: TypeError: User is not a constructor
起初我以为我错误定义了User,但我似乎已根据MongoDB文档正确初始化了它。有什么想法吗?它一直在抛出app.post("/register", (req, res) => {
console.log("entering1");
dataServiceAuth.registerUser(req.body).then((data) => {
res.render('register', {successMessage: "User Created"});
}).catch((err) => {
console.log("Error: " + err);
res.render('register', {errorMessage: err, userName: req.body.userName});
})
});
编辑:/ post / register
</tr>
答案 0 :(得分:0)
我的错误是,
let db = mongoose.createConnection("mongodb://<dbuser>:<dbpassword>@ds237409.mlab.com:37409/web322_a6");
不得使用大于和小于标志。正确的字符串:
let db = mongoose.createConnection("mongodb://dbuser:dbpassword@ds237409.mlab.com:37409/web322_a6");