简体   繁体   English

为什么.save()也不会使用我也告诉过的数据

[英]Why won't .save() use the data I tell it too

Ok so this may be kind of odd, but hear me out. 好的,这可能有点奇怪,但是请听我说。 first thing, this question is related to my issue. 首先,这个问题与我的问题有关。

In a nutshell. 简而言之。 I am trying to make Mongo entries, everything is communicating, but my model.save() only save id and version. 我正在尝试创建Mongo条目,所有内容都在通信,但是我的model.save()仅保存ID和版本。

The difference in what I am trying to do is that I don't want what to send any data. 我尝试执行的操作的区别在于,我不希望发送任何数据。

I want to send a GET request, and I want the server to generate the initial data entry. 我想发送一个GET请求,并且我希望服务器生成初始数据条目。 That entry just being a timestamp and a boolean. 该条目只是一个时间戳和一个布尔值。 So my code looks like this 所以我的代码看起来像这样

  const CLOCK_IN = (req ,res ) => {

      const TimeCard = new TimeCardModel({
        startTime: new Date.now(),
        clockedIn: true
     })
     TimeCard.save()
       .then(res => {
         res.json(TimeCard.toJSON())
       })
        .catch(err => {
         res.json(err)
      })
    }

So what they say in the first question. 所以他们在第一个问题中说了什么。 Is that the object is empty because the function can't read the data. 是该对象为空,因为该函数无法读取数据。 So do I need to write up some code that will make the data I want before I call the entry saving function? 那么,在调用条目保存功能之前,我是否需要编写一些代码以生成所需的数据? Or is there something else that I'm missing? 还是我还缺少其他东西?

My schema was wrong. 我的方案是错误的。

The answer is in the docs 答案在文档中

my model was defined 我的模型已定义

var animalSchema = new Schema({name: { Type: Date },

proper syntax is 正确的语法是

var animalSchema = new Schema({ name: String, type: String });

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

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