Sequelize:插入数组时在 bulkCreate 中出现问题

Sequelize: Issue in bulkCreate while inserting an array

我正在尝试批量插入来自请求正文的数据,但在将其插入数据库时​​遇到了问题。

我试过 bulkCreate(JSON.parse(req.body.myData.toString()) 但没有达到预期效果。

我尝试了与我的问题相关的其他解决方案,但出现了不同类型的错误。

这是我的数据:

myData:
{ id: 121, anotherId: 3},
{ id: 122, anotherId: 3},
{ id: 123, anotherId: 3}

这是我的 bulkCreate 方法:

exports.create = (req, res) => {
    console.log(JSON.parse(req.body.myData.toString()));
    // Save to PostgreSQL database
    MyData.bulkCreate([req.body.myData])
        .then(myData=> {        
            // Send created MyData to client
            res.status(200).json(myData);
        }).catch(err => {
            console.log(err);
            res.status(500).json({msg: "error", details: err});
        });
};

我尝试将 req.body.myData 添加为数组,例如:bulkCreate([req.body.myData])

请告诉我我哪里做错了。

如果您正确定义了 myData 模型,它应该可以工作。

myData 应该是请求正文中的集合。

myData: [
   { id: 121, anotherId: 3},
   { id: 122, anotherId: 3},
   { id: 123, anotherId: 3}
]

然后

MyData.bulkCreate(req.body.myData)
  .then(myData=> {
    .....
  }).catch(err => {
    .....
  });

似乎是我在 Postman 中传递的问题。我将 Header 更改为 Content-Type: application/json 并将数据作为原始格式传递如下:

{ "myData": 
   [ { "id": "121", "anotherId": "3" }, 
     { "id": "122",  "anotherId": "3" }, 
     { "id": "123", "anotherId": "3"} ] 
}