Sequelize bulkCreate returns empty array

Viewed 35

I am trying to post huge amount of data to my database using sequelize bulkCreate. However, I'm getting back an empty array in the console and nothing was submitted to mysql database. code:

const addPerson = async (req, res) => {
  let info = {
    name: req.body.name,
    age: req.body.age,
    location: req.body.location
  };

  const personalInfo = await Person.bulkCreate(info, { validate: true });
  res.status(200).send(personalInfo);
  console.log(personalInfo);
};
 
data coming from postman:

[
  {
    "id": 150,
    "name": "Jim",
    "age": "17",
    "location": "New York"
  },
  {
    "id": 160,
    "name": "Carey",
    "businessId": "18",
    "vatGroup": "Stockholm"
  }
]

response: []

1 Answers

You say that you're trying to post huge amounts of data to your database, but this code you've shared appears to only be posting one Person per call.

The bulkCreate() method's first argument should be an array of data objects. The code you have here should work by just using the create() method, presuming that your model aligns properly with the data you are submitting.

bulkCreate() on the other hand would work with the array of objects that you've shared in the "data coming from postman" section. Just pass that array in as the first argument to bulkCreate() and you should be good.

For further debugging, either use try/catch blocks to capture error messages from sequelize, or use .then()... .catch() instead of async/await.

Related