Express/Mongoose 仅将部分请求数据保存到数据库
Express/Mongoose is only saving partial request data to database
我认为这与我定义模式的方式有关,但我似乎无法找到错误所在...我有一个几乎完全相同的文件设置,但很遗憾我已经无法在任何地方找到此问题的副本。
当通过 Postman 向我的本地 Express 实例发送 API 请求时,只有 'title' 请求 body 值存储在数据库中。我将以下简单请求作为 Application/Json 发送到我的路由(认为使用 x-www-form-urlencoded 时会发生同样的情况):
{
"postTitle": "title goes here",
"postContent": "body goes here",
"isPublished": true
}
这显然是在 express 中注册的,就像我登录 object 我可以看到这个数据(加上时间戳和一个 _id):
{ _id: 5b07d9c0b8124e0599079c04,
postTitle: 'title goes here',
postContent: 'body goes here',
isPublished: true,
createdAt: 2018-05-25T09:39:12.869Z,
updatedAt: 2018-05-25T09:39:12.869Z,
__v: 0 }
但是,当我使用它的 ID 向这个 object 上的路线发送获取请求时,我收到以下响应:
{ "_id": "5b07d9c0b8124e0599079c04" }
同样,如果我发送请求列出所有 object,我会收到以下响应:
{
"posts": [
{
"_id": "5b07d9c0b8124e0599079c04"
},
{
"_id": "5b07d9c0b8124e0599079c03"
},
{
"_id": "5b07d9914f10ce058f137eba"
}
]
}
奇怪的是,有时作为响应的一部分发送的 post 标题会包含在响应中,有时则不会。
我的架构如下:
var postSchema = new Schema({
postTitle: String,
postContent: String,
isPublished: Boolean
},
{
timestamps: true
});
我的 post API POST 请求的路由如下:
router.post('/posts', (req, res, next) => {
var postTitle = req.body.postTitle;
var postContent = req.body.postContent;
var isPublished = req.body.isPublished;
var newPost = new Post({
postTitle: postTitle,
postContent: postContent,
isPublished: isPublished
});
newPost.save(function (error) {
if (error) {
console.log(error)
}
res.send({
success: true,
message: 'Post saved successfully!'
})
})
});
(如果您不使用路由器,您将使用 'app.post' 而不是 'router.post')同样,这有点冗长,但一切正常。
我的GET路由如下:
router.get('/posts', (req, res) => {
Post.find({}, 'title content published', function (error, posts) {
if (error) { console.error(error); }
res.send({
posts: posts
})
}).sort({_id:-1})
});
好的 - 因此,通过详细检查我的代码,我已经找出了我出错的地方并解决了问题,但是,在我的搜索中,我发现的结果很少。我是 Express 的新手,所以我将概述问题的原因以及我是如何解决它的,以便在其他人犯同样的愚蠢错误时为他们节省大量时间。
现在,我遇到的问题是我检索数据并响应 get 请求提供数据的方式造成的。例如,这是我的 GET 路由,用于列出所有 objects.
我完全专注于 post 请求并假设它是数据库的问题。事实证明,我实际上所做的是为了使我的模式和路由不那么混乱,我更改了相关变量的名称。但是,我忘记做的是更新我的 GET 路由中的这一行以反映更改:
Post.find({}, 'postTitle postContent isPublished', function (error, posts) {
我留下的是:
Post.find({}, 'title content published', function (error, posts) {
有时显示标题的原因是我尝试来回撤消更改以发现问题。
我知道这是一个超级基本的查询,但我在一天中的大部分时间都被困在这个问题上,唯一的其他相关讨论以 OP 说它神奇地自行修复而告终。
我认为这与我定义模式的方式有关,但我似乎无法找到错误所在...我有一个几乎完全相同的文件设置,但很遗憾我已经无法在任何地方找到此问题的副本。
当通过 Postman 向我的本地 Express 实例发送 API 请求时,只有 'title' 请求 body 值存储在数据库中。我将以下简单请求作为 Application/Json 发送到我的路由(认为使用 x-www-form-urlencoded 时会发生同样的情况):
{
"postTitle": "title goes here",
"postContent": "body goes here",
"isPublished": true
}
这显然是在 express 中注册的,就像我登录 object 我可以看到这个数据(加上时间戳和一个 _id):
{ _id: 5b07d9c0b8124e0599079c04,
postTitle: 'title goes here',
postContent: 'body goes here',
isPublished: true,
createdAt: 2018-05-25T09:39:12.869Z,
updatedAt: 2018-05-25T09:39:12.869Z,
__v: 0 }
但是,当我使用它的 ID 向这个 object 上的路线发送获取请求时,我收到以下响应:
{ "_id": "5b07d9c0b8124e0599079c04" }
同样,如果我发送请求列出所有 object,我会收到以下响应:
{
"posts": [
{
"_id": "5b07d9c0b8124e0599079c04"
},
{
"_id": "5b07d9c0b8124e0599079c03"
},
{
"_id": "5b07d9914f10ce058f137eba"
}
]
}
奇怪的是,有时作为响应的一部分发送的 post 标题会包含在响应中,有时则不会。
我的架构如下:
var postSchema = new Schema({
postTitle: String,
postContent: String,
isPublished: Boolean
},
{
timestamps: true
});
我的 post API POST 请求的路由如下:
router.post('/posts', (req, res, next) => {
var postTitle = req.body.postTitle;
var postContent = req.body.postContent;
var isPublished = req.body.isPublished;
var newPost = new Post({
postTitle: postTitle,
postContent: postContent,
isPublished: isPublished
});
newPost.save(function (error) {
if (error) {
console.log(error)
}
res.send({
success: true,
message: 'Post saved successfully!'
})
})
});
(如果您不使用路由器,您将使用 'app.post' 而不是 'router.post')同样,这有点冗长,但一切正常。
我的GET路由如下:
router.get('/posts', (req, res) => {
Post.find({}, 'title content published', function (error, posts) {
if (error) { console.error(error); }
res.send({
posts: posts
})
}).sort({_id:-1})
});
好的 - 因此,通过详细检查我的代码,我已经找出了我出错的地方并解决了问题,但是,在我的搜索中,我发现的结果很少。我是 Express 的新手,所以我将概述问题的原因以及我是如何解决它的,以便在其他人犯同样的愚蠢错误时为他们节省大量时间。
现在,我遇到的问题是我检索数据并响应 get 请求提供数据的方式造成的。例如,这是我的 GET 路由,用于列出所有 objects.
我完全专注于 post 请求并假设它是数据库的问题。事实证明,我实际上所做的是为了使我的模式和路由不那么混乱,我更改了相关变量的名称。但是,我忘记做的是更新我的 GET 路由中的这一行以反映更改:
Post.find({}, 'postTitle postContent isPublished', function (error, posts) {
我留下的是:
Post.find({}, 'title content published', function (error, posts) {
有时显示标题的原因是我尝试来回撤消更改以发现问题。
我知道这是一个超级基本的查询,但我在一天中的大部分时间都被困在这个问题上,唯一的其他相关讨论以 OP 说它神奇地自行修复而告终。