简体   繁体   English

Express / Mongoose仅将部分请求数据保存到数据库

[英]Express/Mongoose is only saving partial request data to database

I think this is related to how I've defined my schemas, but I can't seem to find where the bug is... I have an almost identical file set up that's working perfectly and I've unfortunately not been able to find a duplicate of this issue anywhere. 我认为这与我定义模式的方式有关,但是我似乎找不到错误的位置...我设置了几乎相同的文件,并且运行良好,但是很遗憾,我找不到此问题在任何地方都是重复的。

When sending an API request to my local Express instance via Postman, only the 'title' request body value is stored in the database. 通过Postman向本地Express实例发送API请求时,数据库中仅存储“ title”请求主体值。 I am sending the following simple request to my route as Application/Json (thought the same happens when using x-www-form-urlencoded): 我将以下简单请求作为Application / Json发送到我的路由(使用x-www-form-urlencoded时会发生相同的情况):

{
    "postTitle": "title goes here",
    "postContent": "body goes here",
    "isPublished": true
}

This is clearly being registered in express, as if I log the object I can see this data (plus timestamps and an _id): 这显然是以快速方式注册的,就好像我登录该对象时,我可以看到此数据(加上时间戳和_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 }

However, when I send a get request to my route on this object using its ID, I receive the following in response: 但是,当我使用该对象的ID向该对象的路由发送get请求时,会收到以下响应:

{ "_id": "5b07d9c0b8124e0599079c04" }

Likewise, if I send a request to list all objects, I receive the following response: 同样,如果我发送列出所有对象的请求,则会收到以下响应:

{
    "posts": [
        {
            "_id": "5b07d9c0b8124e0599079c04"
        },
        {
            "_id": "5b07d9c0b8124e0599079c03"
        },
        {
            "_id": "5b07d9914f10ce058f137eba"
        }
    ]
}

Weirdly, sometimes the post title sent as part of the response is included in the response, and sometimes it isn't. 奇怪的是,有时作为响应的一部分发送的帖子标题包含在响应中,有时却没有。

My schema is as follows: 我的架构如下:

var postSchema = new Schema({
  postTitle: String,
  postContent: String,
  isPublished: Boolean
},
{
  timestamps: true
});

My post API route for POST requests is as follows: 我的POST请求的post API路由如下:

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!'
    })
  })
});

(If you're not using Router, you'll have 'app.post' instead of 'router.post') Again, this is a bit longwinded but everything works fine. (如果您不使用路由器,则将使用“ app.post”而不是“ router.post”。)再次,这有点冗长,但是一切正常。

My GET route is as follows: 我的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})
});

OK - so, by going through my code in detail I've figured out where I was going wrong and fixed the issue, however, in my searching I found very little in the way of results. 好的-因此,通过详细检查我的代码,我找出了哪里出了问题并解决了问题,但是,在搜索中,我发现结果的方式很少。 I'm pretty new to Express, so I'm going to outline the cause of the issue and how I resolved it in order to potentially save someone else a bunch of time if they make the same stupid mistake. 我是Express的新手,所以我将概述问题的原因以及解决方案,以便在其他人犯同样愚蠢的错误时节省大量时间。

Now, the issue I'm having results from the way I was retrieving the data and serving that in response to get requests. 现在,我遇到的问题是我检索数据并响应请求的方式提供的结果。 As an example, here's my GET route to list all of the objects. 例如,这是我列出所有对象的GET路线。

I was entirely focusing on the post request and assuming it was a problem with the database. 我完全专注于发布请求,并假设这是数据库的问题。 It turns out what I'd actually done, is in order to make my schemas and routes less confusing, I'd changed the names of the relevant variables. 事实证明,我实际上所做的是为了使我的架构和路由更不混乱,我更改了相关变量的名称。 What I'd forgotten to do, however, is update this line in my GET route to reflect the change: 但是,我忘记要做的是在GET路由中更新此行以反映更改:

  Post.find({}, 'postTitle postContent isPublished', function (error, posts) {

Which I'd left as: 我留下的是:

  Post.find({}, 'title content published', function (error, posts) {

The reason the title sometimes displayed is that I tried undoing changes back and forth to spot the issue. 标题有时显示的原因是我试图来回撤消更改以发现问题。

I know this is a super basic query but I got stuck on this for the best part of a day, and the only other relevant discussion on this ended with OP saying that it magically fixed itself. 我知道这是一个超级基本的查询,但是在一天的大部分时间里我都坚持了这一点,与此有关的唯一其他相关讨论都以OP结尾,说它神奇地修复了自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM