[英]Mongoose Not Saving Data
I am having trouble with a simple query on my database.我在对我的数据库进行简单查询时遇到问题。 Following this tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 when Model.find() is called, he receives a JSON object back with the name field (the only custom field) and the _id and __v.按照本教程: https : //scotch.io/tutorials/build-a-restful-api-using-node-and-express-4当 Model.find() 被调用时,他收到一个带有 name 字段的 JSON 对象(唯一的自定义字段)以及 _id 和 __v。 When I do the same, all I receive back is the _id and __v field.当我这样做时,我收到的只是 _id 和 __v 字段。 I do get a successful response back saying the post was created, but it doesn't include the title or content fields.我确实得到了一个成功的回复,说帖子已创建,但它不包括标题或内容字段。 Yet a query shows that the data was never saved.然而,查询显示数据从未被保存。
Routing and query:路由和查询:
var express = require("express");
var router = express.Router();
var Post = require("../app/models/post.js");
/* Drop Post collection
Post.remove({}, function(err, num_docs) {
if (err) {
res.send(err);
} else {
console.log("Collection dropped, documents deleted: " + num_docs);
}
});
*/
// Middleware for all routes.
router.use(function(req, res, next) {
console.log("API request made.");
next(); // Go to next routes, don't stop here
});
// Test route to ensure routing is working
router.get("/", function(req, res) {
res.json({
message: "Hooray! Welcome to the API!"
});
});
// On routes that end in /posts
router.route("/posts")
// Create post. (Accessed at POST http://localhost/api/posts)
.post(function(req, res) {
var post = new Post(); // Create new instance of post model
post.title = req.body.title; // Set title (from request)
post.content = req.body.content; // Set content (from request)
// Save the post, and check for errors.
post.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({
message: "Post created!",
title: post.title,
content: post.content
});
}
});
})
.get(function(req, res) {
Post.find({}).exec(function(err, posts) {
if(err) {
res.send(err);
} else {
res.json(posts);
}
});
});
module.exports = router;
Response:回复:
[
{
"_id": "56a6adc31f06c4dc1cf82888",
"__v": 0
},
{
"_id": "56a9768888f806dc1fe45415",
"__v": 0
},
{
"_id": "56a97f3f4e269b7c21311df8",
"__v": 0
}
]
A db query in the shell returns the same information, just an _id and __v field. shell 中的 db 查询返回相同的信息,只是一个 _id 和 __v 字段。
I am beyond confused right now.我现在非常困惑。 It suddenly works, the code is the exact same as above.它突然起作用了,代码与上面完全相同。 I am going to leave this open in case someone stumbles across it one day and can solve this mystery.如果有一天有人偶然发现并可以解开这个谜团,我将保持开放状态。
问题是您需要在将发布请求作为application/json
发送时设置content-type
,否则无法识别字段。
I experienced an error like this.我遇到了这样的错误。
The problem was that I was loading the wrong mongoose Schema.问题是我加载了错误的猫鼬模式。
The result is that the only fields that are saved are those that are present in both schemas, ie _id
and __v
.结果是唯一保存的字段是存在于两个模式中的字段,即_id
和__v
。
For this code对于此代码
post.title = req.body.title; // Set title (from request) post.content = req.body.content; // Set content (from request)
could you check你能检查一下吗
req.body.title
and req.body.content
are not undefined
? req.body.title
和req.body.content
不是undefined
?do you set the field in your Post schema as您是否将 Post 架构中的字段设置为
var PostSchema = new Schema({ title: String, content: String }); var PostSchema = new Schema({ title: String, content: String });
If you are using a manual tool like Postman to test your app, you must also put quotes around the key names in the body of your request, like {"key": "some string"}
.如果您使用 Postman 等手动工具来测试您的应用程序,您还必须在请求正文中的键名周围加上引号,例如{"key": "some string"}
。
If you just put {key: "some string"}
then the whole key/value pair is ignored when the document is saved to the database.如果您只输入{key: "some string"}
那么在将文档保存到数据库时,整个键/值对将被忽略。
Exact same thing happened to me...完全一样的事情发生在我身上...
First two POSTs succeeded but did not post the data I was sending:前两个 POST 成功但没有发布我发送的数据:
var p = new Post();
p.result = 'hello-world';
p.save(function (err) {});
Turned on debug mode: mongoose.set('debug', true);
开启调试模式: mongoose.set('debug', true);
and next POST the field was saved...和下一个 POST 字段被保存...
Baffling!莫名其妙!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.