1

I am trying to insert a lot of data i database using Sequelize library but it is showing some strange as I am passing an array of objects into the bulkCreate query .

I have function something like this

db.organizations
          .findAll({
            attributes: ['name'],
            where: {
              id: req.ORG_ID
            }
          })
          .then(org => {
            let i; let smsData = [];
            for (i = 0; i < user.length; i++) {
              let DefaultText = Utils.defaultMessage(
                user[i].name,
                user[i].dataValues.amount,
                org[0].name
              );
              smsData.push({
                organizationId:
                  user[i].dataValues.organizationEntries[0].organizationId,
                mobile: user[i].mobile,
                message: DefaultText,
                status: 0
              });
            }
            console.log(JSON.stringify(smsData));
            db.logSms.bulkCreate([{
              smsData
            }]).then(() => { // Notice: There are no arguments here, as of right now you'll have to...
              return db.logSms.findAll({ where: { status: 0 } });
            }).then(sms => {
              console.log(sms)
            })

and I have also done console.log(smsData) which is showing array of objects but I am not able to understand why I am facing this error.

INSERT INTO log_sms (id,created_at,updated_at) VALUES (NULL,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP); Unhandled rejection SequelizeDatabaseError: Field 'mobile' doesn't have a default value

This error is basically coming as while executing the query as u can see below the error as I have made mobile field compulsory and I am passing the array of objects which is having mobile number but still this error is coming. Please give some hint

1 Answer 1

0

I have got my mistake as I should remove the braces from this statement:

db.logSms.bulkCreate([{ smsData }])

it should be like this:

db.logSms.bulkCreate(smsData)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.