Javascript 如何使用Sequelize批量更新数组中的元素?

Javascript 如何使用Sequelize批量更新数组中的元素?,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,我有一个帐户数据库。我需要更新每个帐户中不同的字段。 该帐户如下所示: const obj = { id: 492, type: 'sel', username: 'username@outlook.com', password: 'password', proxy: { ip: 'some_ip', type: 'http', port: '222', datacenter: null, targeting: 'country',

我有一个帐户数据库。我需要更新每个帐户中不同的字段。 该帐户如下所示:

const obj = {
  id: 492,
  type: 'sel',
  username: 'username@outlook.com',
  password: 'password',
  proxy: {
    ip: 'some_ip',
    type: 'http',
    port: '222',
    datacenter: null,
    targeting: 'country',
    username: 'username',
    password: 'ffsfs',
    provider: 'noname'
  },
}
我可以通过以下方式为一个帐户更新这些字段:

db.Account.findById(492)
.then((a) => {
     let countryCode = a.country.toUpperCase();
     let country = getName(countryCode).replace(/\s/g, '');
     let sessionId = randomstring.generate({
       charset: 'alphanumeric',
       length: 6
     });
     const proxyPassword = `${country}_ses-${sessionId}`;
     
     let newProxy = Object.assign({}, a.proxy);

     newProxy.provider = 'provider';
     newProxy.password = proxyPassword;
     newProxy.username = 'username';
     newProxy.port = '121212';
     newProxy.ip = 'proxy.ip';

     return a.update({
       proxy: newProxy
     });
 }).catch((e) => {
     console.log(e);
   })

db.Account.findAll({
    where: { 
      id: [492, 572, 2]
    },  raw: true 
  })
    .then((accounts) => {
      accounts.forEach((a) => {
        let countryCode = a.country.toUpperCase();
        let country = getName(countryCode).replace(/\s/g, '');
        let sessionId = randomstring.generate({
          charset: 'alphanumeric',
          length: 6
        });
        const proxyPassword = `${country}_ses-${sessionId}`;
        
        let newProxy = Object.assign({}, a.proxy);

        newProxy.provider = 'provider';
        newProxy.password = proxyPassword;
        newProxy.username = 'username';
        newProxy.port = '121212';
        newProxy.ip = 'proxy.ip';

        return a.update({
          proxy: newProxy
        });
 }).catch((e) => {
     console.log(e);
   })

但当我尝试对数组执行相同操作时,控制台中会出现一个错误:

TypeError:a.update不是一个函数

我试着这样做:

db.Account.findById(492)
.then((a) => {
     let countryCode = a.country.toUpperCase();
     let country = getName(countryCode).replace(/\s/g, '');
     let sessionId = randomstring.generate({
       charset: 'alphanumeric',
       length: 6
     });
     const proxyPassword = `${country}_ses-${sessionId}`;
     
     let newProxy = Object.assign({}, a.proxy);

     newProxy.provider = 'provider';
     newProxy.password = proxyPassword;
     newProxy.username = 'username';
     newProxy.port = '121212';
     newProxy.ip = 'proxy.ip';

     return a.update({
       proxy: newProxy
     });
 }).catch((e) => {
     console.log(e);
   })

db.Account.findAll({
    where: { 
      id: [492, 572, 2]
    },  raw: true 
  })
    .then((accounts) => {
      accounts.forEach((a) => {
        let countryCode = a.country.toUpperCase();
        let country = getName(countryCode).replace(/\s/g, '');
        let sessionId = randomstring.generate({
          charset: 'alphanumeric',
          length: 6
        });
        const proxyPassword = `${country}_ses-${sessionId}`;
        
        let newProxy = Object.assign({}, a.proxy);

        newProxy.provider = 'provider';
        newProxy.password = proxyPassword;
        newProxy.username = 'username';
        newProxy.port = '121212';
        newProxy.ip = 'proxy.ip';

        return a.update({
          proxy: newProxy
        });
 }).catch((e) => {
     console.log(e);
   })

为什么会这样?请帮忙

更新。 我试过这样做,但似乎奏效了:

 db.Account.findAll({
    where: { 
      id: [492, 572, 2]
    },  raw: true 
  })
    .then((accounts) => {
      let updates = accounts.map((a) => {
        let countryCode = a.country.toUpperCase();
        let country = getName(countryCode).replace(/\s/g, '');
        let sessionId = randomstring.generate({
          charset: 'alphanumeric',
          length: 6
        });
        const proxyPassword = `${country}_ses-${sessionId}`;
        
        let newProxy = Object.assign({}, a.proxy);

        newProxy.provider = 'provider';
        newProxy.password = proxyPassword;
        newProxy.username = 'username';
        newProxy.port = '121212';
        newProxy.ip = 'proxy.ip';

        db.Account.findById(a.id)
        .then((item) => {
          item.update({
            proxy: newProxy
          });
        })
      })
    Promise.all(updates)
      .catch((e) => {
        console.log(e);
      })


您不能在同步循环中调用异步函数,即使它看起来没有解析模型,而且Sequelize to bulk update data中还有最后一个解决方法,因此不需要执行多次更新

db.Account
  .findAll({
    where: {
      id: [492, 572, 2]
    },
    raw: true
  })
  .then((accounts) => {
    const data = accounts.map((a) => {
      let countryCode = a.country.toUpperCase();
      let newProxy = Object.assign({}, a.proxy);
      newProxy.provider = 'provider';
      a.countryCode = countryCode;
      a.proxy = newProxy;
      // change all data then return the new account and it must include the id attribute
      return a;
    });

    return db.Account.bulkCreate(data, { updateOnDuplicate: ['id'] });
  })
  .catch((e) => {
    console.log(e);
  });
供参考:


感谢您提供的详细答案,这让我更清楚了!但不幸的是,我的数据库是旧版本的postgres。我没有办法更新它。请查看我的回复更新。此方法可接受吗?Sequelize
bulkCreate
将执行多个查询以更新记录,因此它将完全执行您想要的操作,但使用一行代码,强烈建议利用使用的ORM方法,而不是编写自己的ORM方法。您在尝试使用它时是否出现任何错误?未出现任何错误。只是我的sequelize和postgres版本不支持bulkCreate方法。无论如何,非常感谢你的帮助!不客气,如果有帮助,请将答案标记为正确。