【问题标题】:Unable to post Array value pairs to mongoDb via postman and using mongoose无法通过邮递员和使用 mongoose 将数组值对发布到 mongoDb
【发布时间】:2020-08-26 20:01:33
【问题描述】:

当我尝试这样做时,我会发布其余的数据,但数组仍然为空

我的 Schma 看起来像这样

const mongoose = require('mongoose');

const DocumentSchema = mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users',
  },
  dateCreated: String,
  title: String,
  // ID of the users that created the document
  creatorId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users',
  },






  comment: [{ title: String, detail: String }]



});

module.exports = mongoose.model('myDocument', DocumentSchema);

我在 postman 中的 Post 脚本是这样的

{
    "label":"Got More data",
    "linkUrl":"www.google.com",
    "title":"test 14",
    "comment":[
        {
            "title":"hello",
            "detail":"some detail"
        }]
}

当我得到我更新的数据时,它看起来像这样

  {
        "_id": "5eb91c0e35eecb369696b9c5",
        "title": "test 14",
        "creatorId": "5d8856e5903a260588b77c30",
        "user": "5d8856e5903a260588b77c30",
        "dateCreated": "Mon May 11 2020 10:34:06 GMT+0100 (British Summer Time)",
        "comment": [],
        "__v": 0
    }

我尝试过的其他事情

我已尝试根据 Stack Overflow How to define object in array in Mongoose schema correctly with 2d geo index 上的这篇文章更改架构定义

到这里

comment : [{
    title : String,
    detail : String
     }]

还有这种方法

 comment :  {
             "title":"A title",
             "detail": "some random comment"
           }

但我仍然得到一个空白数组

【问题讨论】:

    标签: mongodb postman mongoose-schema


    【解决方案1】:

    问题出在 react api 设置中,我没有将文件添加到下面的 api 代码中是包含“comment”字段的新代码(在 imageUrl 下)。现在可以使用了

    router.post(
      '/',
      [
        auth,
        [
          check('title', ' myDocument Title is required')
            .not()
            .isEmpty()
        ]
      ],
      async (req, res) => {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
          return res.status(400).json({ errors: errors.array() });
        }
    
        const { title, summary, imageURL, template, comment } = req.body;
    
        try {
          const newMyDocument = new MyDocument({
            title,
            summary,
            imageURL,
            comment,
            template,
            creatorId: req.user.id,
            user: req.user.id,
            columnOrder:["col1","col2","col3"],
            columns:[
              {col_1 :
                {
                  id:"col_1",
                  title:"facts",
                  taskIds:["test1","test2"]
                }
              },
              {col_2 :
                {
                  id:"col_2",
                  title:"Pros",
                  taskIds:["test1","test2"]
                }
              },
              {col_3 :
                {
                  id:"col_3",
                  title:"Cons",
                  taskIds:["test1","test2"]
                }
              }
            ],
            dateCreated:new Date()
          });
          const myDocument = await newMyDocument.save();
          ///this is the request to othe server
    
          res.json(myDocument);
        } catch (err) {
          res.status(500).send('Server Error 76');
        }
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 2021-12-11
      • 1970-01-01
      • 2019-10-23
      • 2023-02-20
      • 2020-06-23
      • 2016-02-03
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多