【发布时间】:2019-05-20 03:05:42
【问题描述】:
我是 node.js 的新手。 我收到了这条消息:
TypeError: Cannot read property 'then' of undefined
代码如下:
router.post("/signup", (req, res) => {
const userRegister = new UserRegister({
_id: new mongoose.Types.ObjectId(),
nickname: req.body.nickname,
email: req.body.password,
password: req.body.password,
level: 0
});
console.log(req.body.nickname + " " + req.body.email + " " + req.body.password);
userRegister
.save()
.then(doc => {
console.log(doc);
res.status(200).json({
message: "User created"
});
})
.catch(err => {
if (err)
console.log("error => " + err)
res.status(409).json({
message: "ERROR"
})
});
});
和架构:
const mongoose = require("mongoose");
const userRegister = mongoose.Schema({
_id : mongoose.Schema.Types.ObjectId,
nickname: String,
email: String,
password: String,
level: Number
});
module.exports = mongoose.model("UserRegister", userRegister);
我真的不明白为什么它说“.then undefined”。 (身材不错)
【问题讨论】:
-
UserRegister定义在哪里? -
您好!我更新了你现在可以看到的帖子
-
更好的缩进会让它更容易阅读
-
你好,在代码里!
-
澄清一下,“.then”不是未定义的。它告诉你你试图调用“.then”的对象是未定义的,这是一条有价值的信息。 ".then" 将在 Promise 解析后使用,因此请尝试将 UserRegister 的声明包装在 Promise 中。
标签: javascript node.js express mongoose promise