Angular 使用从角度组件调用的Mongoose子文档数组更新文档失败

Angular 使用从角度组件调用的Mongoose子文档数组更新文档失败,angular,mongoose,mean-stack,Angular,Mongoose,Mean Stack,在MEAN stack中,我试图从Angular更新mongoose文档,但没有得到预期的结果:只更新文档,而不更新关联的子文档(值替换为null) 我在mongoose中使用以下模型: /* Mongoose file - thread.model.js */ var ThreadSchema = new mongoose.Schema({ category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', requ

在MEAN stack中,我试图从Angular更新mongoose文档,但没有得到预期的结果:只更新文档,而不更新关联的子文档(值替换为null)

我在mongoose中使用以下模型:

/* Mongoose file - thread.model.js */
var ThreadSchema = new mongoose.Schema({
  category: {
    type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true
  },
  title: { type: String, required: true },
  messages: [{
      type: mongoose.Schema.Types.ObjectId, ref: 'Message', required: true
  }],
  numberOfViews: { type: Number, required: true, default: 0 },
  numberOfAnswers: { type: Number, required: true, default: 0 },
  numberOfLikes: { type: Number, required: true, default: 0 },
  numberOfThanks: { type: Number, required: true, default: 0 },
  tags: [{
      type: mongoose.Schema.Types.ObjectId, ref: 'Tag'
  }]
}, { timestamps: { createdAt: 'createdAt' } });
我用Postman测试了这个更新查询,结果很成功,可能不是更优化的,但似乎很有效:

/* Express & Mongoose file - threads.route.js */
router.put('/:threadId', function(req, res, next) {
  Thread.findByIdAndUpdate(req.params.threadId, { title: req.body.title },
    function(err, thread) {
      if (err) return next(err);

      Message.findByIdAndUpdate(thread.messages[0], { text: req.body.text }, 
        function(err, message) {
          if (err) return next(err);
      });
      res.json(thread);
  });
});
现在,在Angular中,我有一个组件应该在用户提交带有以下代码的被动表单时更新我的线程:

/* Angular file - threads-edit.component.ts */
onSubmit(): void {
    // Set the data model with its corresponding form model
    this.thread.title = this.threadForm.value.title;
    this.thread.messages[0].text = this.threadForm.value.text;

    this.threadService.updateThread(this.thread)
      .subscribe(() => this.goBack());
}
submit方法使用update方法调用threadService,update方法只调用线程的API put:

 /* Angular file - thread.service.ts */
 updateThread(thread: Thread): Observable<any> {
   const id = thread._id;
   const url = `${this.threadsUrl}/${id}`;

   return this.http.put(url, thread, httpOptions).pipe(
     catchError(this.handleError<any>('updateThread'))
   );
 }
更新后,请执行以下操作:

{
    "messages": [
        {
            "comments": [],
            "_id": "5b25e52eeb7f580b59d6a431",
            "text": null,
            "createdAt": "2018-06-17T04:35:58.284Z",
            "updatedAt": "2018-06-17T04:40:20.351Z",
            "__v": 0
        }
    ],
    "numberOfViews": 0,
    "numberOfAnswers": 0,
    "numberOfLikes": 0,
    "numberOfThanks": 0,
    "tags": [],
    "_id": "5b25e52deb7f580b59d6a430",
    "category": "5b25e520eb7f580b59d6a42e",
    "title": "Bar",
    "createdAt": "2018-06-17T04:35:57.738Z",
    "updatedAt": "2018-06-17T04:40:20.341Z",
    "__v": 1
}
我希望我的解释足够清楚


提前感谢你的帮助

我假设您正在将
线程作为
请求体传递
req.body.title
=
thread.title
但我不认为
req.body.text
=
thread.text
!?因此它是
null
。新线程发布后的
和更新后的
线程
没有相同的
\u id
?这两条消息应该是相同的吗?你是对的,
req.body.text
=
this.thread.messages[0].text
,因为这是我要更改的线程的第一条消息。我在代码中更改了它,现在它开始工作了!谢谢,您需要使用操作符来更新mongodb中的数组
{
    "messages": [
        {
            "comments": [],
            "_id": "5b25e52eeb7f580b59d6a431",
            "text": null,
            "createdAt": "2018-06-17T04:35:58.284Z",
            "updatedAt": "2018-06-17T04:40:20.351Z",
            "__v": 0
        }
    ],
    "numberOfViews": 0,
    "numberOfAnswers": 0,
    "numberOfLikes": 0,
    "numberOfThanks": 0,
    "tags": [],
    "_id": "5b25e52deb7f580b59d6a430",
    "category": "5b25e520eb7f580b59d6a42e",
    "title": "Bar",
    "createdAt": "2018-06-17T04:35:57.738Z",
    "updatedAt": "2018-06-17T04:40:20.341Z",
    "__v": 1
}