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'}
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

orderId[0] , is working, that not what i need .. i need to update multiple rows .. so id should be array or objective. but it wan't work
what does it mean by [Op.or]
@devakadabare [Op.or] is the or operator for where condition. It won't help you. check the updated answer.
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});

    }
    );  

  }

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.