【发布时间】:2020-07-23 01:39:26
【问题描述】:
我正在使用 Postman 来了解 API。我的 server.js 文件的内容在下面的代码中。但是,当我通过 Postman 发送帖子时,错误“无法读取未定义的属性 'title'”一直显示。
var Product = require("./model/product");
var WishList = require("./model/wishlist");
app.post("/product", function (request, response) {
var product = new Product();
product.title = request.body.title;
product.price = request.body.price;
product.save(function (err, savedProduct) {
if (err) {
response.status(500).send({ error: "Could not save product" });
} else {
response.status(200).send(savedProduct);
}
});
});
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.listen(3000, function () {
console.log("Swag Shop API runing on port 3000...");
});
product.js 文件包含以下代码。
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var product = new Schema({
title: String,
price: Number,
likes: { type: Number, default: 0 },
});
module.exports = mongoose.model("Product", product);
我尝试通过 Postman 发送以下 json 文件,但随后显示错误类型:“无法读取未定义的属性 'title'”。
{
"title": "Test Title",
"price": 100.00
}
这些是查看我的文件位置的文件夹:Folders。
这个solution 使用npm install express@">=3.0.0 <4.0.0" --save 在我的情况下不起作用。在我的终端中使用它后,同样的错误一直显示。
我该如何解决这个问题?
【问题讨论】:
-
我会尝试在
app.post()成功回调中执行console.log(request),以查看它是否具有所需的属性。 -
您使用服务器和客户端调试器吗?
标签: javascript node.js mongodb mongoose postman