0

I am trying to update multiple rows using Sequelize. But, it will give me Error .

SequelizeValidationError","errors":
    [{"message":"id cannot be an array or an object",
        "type":"string violation",
        "path":"id",
        "value":["ord-307486d0-10cb-11ea-8174-9b8214c20c49","ord-ef0f0a80-10ca-11ea-8174-9b8214c20c49"],
        "origin":"CORE",
        "instance":{"id":["ord-307486d0-10cb-11ea-8174-9b8214c20c49","ord-ef0f0a80-10ca-11ea-8174-9b8214c20c49"],
        "status":"PAID",
        "updatedAt":"2019-11-28T03:47:38.057Z"},
        "validatorKey":"not_a_string",
        "validatorName":null,
        "validatorArgs":[]}]},
    "isUpdated":false

Can you help me to figure it out? How can i do this?

This is my code :

updateOrder: (orderData, vendorId, callback) => {

  models.Orders.update(orderData, {
      where: {
        id: orderData.id
      }
    })
    .then((result) => {

      if (result != '0') {
        logger.log({
          level: 'info',
          message: {
            user: vendorId,
            request: {
              orderData
            },
            response: {
              updated: true
            },
            service: 'updateOrder',
            date: date,
            type: 'UPDATE'
          }
        });
        callback({
          statusCode: Constants.errorStatus.SUCCESS,
          body: orderData,
          isUpdated: true
        });
      } else {
        logger.log({
          level: 'warn',
          message: {
            user: vendorId,
            request: {
              orderData
            },
            response: {
              updated: false,
              details: 'Id isnt found'
            },
            service: 'updateOrder',
            date: date,
            type: 'UPDATE'
          }
        });
        callback({
          statusCode: Constants.errorStatus.NOT_FOUND,
          body: 'Id is not matching',
          isUpdated: false
        });
      }

    }).catch((error) => {
      logger.log({
        level: 'error',
        message: {
          user: vendorId,
          request: {
            orderData
          },
          response: {
            error
          },
          service: 'updateOrder',
          date: date,
          type: 'UPDATE'
        }
      });
      callback({
        statusCode: Constants.errorStatus.BAD_REQUEST,
        body: error,
        isUpdated: false
      });

    });

},

My sending object is like this

orderData={id:purchaseData.orderId,status:'PAID'}
purchaseData.orderId={'ord001','ord002'}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
devaka dabare
  • 39
  • 1
  • 10
  • Does this answer your question? [Update multiple rows in sequelize with different conditions](https://stackoverflow.com/questions/49643047/update-multiple-rows-in-sequelize-with-different-conditions) – Roshana Pitigala Nov 28 '19 at 04:42

2 Answers2

0

The problem lies in your orderData object. You are trying something like this.

models.Orders.update({id:{'ord001','ord002'},status:'PAID'}, {
  ...
})

id: should be a single value like {id:'ord001',status:'PAID'}


You can update multiple records, but same updates for all records. Your id updates are different. So you'll have to call update multiple times. Read this answer.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
0
updateOrders: (orderData, vendorId ,callback) => {

    const query='UPDATE Orders SET status="'+orderData.status+'" WHERE id IN ('+orderData.id+');';
    sequelize.query(query,
    { type: sequelize.QueryTypes.UPDATE }
    ).then(result => {
      if(result!='0'){
        logger.log({level: 'info', message: {user:vendorId ,request : {query} , response: {updated:true} ,service:'updateOrder', date:date ,type:'UPDATE'}});
        callback({statusCode: Constants.errorStatus.SUCCESS, body: orderData ,isUpdated:true});
      }else{
        logger.log({level: 'warn', message: {user:vendorId ,request : {query} , response: {updated:false ,details:'Id isnt found'} ,service:'updateOrder', date:date ,type:'UPDATE'}});
        callback({statusCode: Constants.errorStatus.NOT_FOUND, body: 'Id is not matching' ,isUpdated:false});
      } 

    }).catch((error) => {
      logger.log({level: 'error', message: {user:vendorId ,request : {query} , response: {error} ,service:'updateOrder', date:date ,type:'UPDATE'}});
      callback({statusCode: Constants.errorStatus.BAD_REQUEST, body: error ,isUpdated:false});

    }
    );  

  }
tuhin47
  • 5,172
  • 4
  • 19
  • 29
devaka dabare
  • 39
  • 1
  • 10