【发布时间】:2020-06-23 06:45:53
【问题描述】:
我用更多信息更新了这个问题:“ 我对 Express/mongodb 还很陌生,我正在尝试使用邮递员将我的 userSchema 发送到 mongoDb,但我收到此错误:"
mongoose
.connect(config.mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("db connected"))
.catch(err => console.log(err));
app.use(cors());
app.options("*", cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.post("/api/users/register", (req, res) => {
const user = new User(req.body);
user.save((err, userData) => {
if (err) return res.json({ success: false, err });
});
return res.status(200);
});
app.listen(5000);
这是用户架构:
const mongoose = require("mongoose");
const userSchema = mongoose.Schema({
name: {
type: String,
maxlength: 50
},
email: {
type: String,
trim: true,
unique: 1
},
password: {
type: String,
minlength: 5
},
lastname: {
type: String,
maxlength: 50
},
role: {
type: Number,
default: 0
},
token: {
type: String
},
tokenExp: {
type: Number
}
});
const User = mongoose.model("User", userSchema);
module.exports = { User };
请问我做错了什么?
【问题讨论】:
-
首先将 GET 方法改为 postman 中的 POST。其次在正文中,请确保发送 JSON。第三,将
return res.status(200);行放在 if (err) 行之后 -
可以在 Postman 中添加“正文”选项卡的屏幕截图吗?我怀疑您的 JSON 中有错误
标签: node.js mongodb express mongoose postman