如何使用 Sequelize 批量更新数组中的元素?
How can I mass-update elements in an array using Sequelize?
我有一个带有帐户的数据库。我需要将字段更新为每个帐户中的不同字段。
该帐户看起来像这样:
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);
})
但是当我尝试对数组执行相同操作时,控制台中出现错误:
TypeError: a.update 不是函数
我正在尝试这样做:
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 中有一个解决方法来批量更新数据,所以不需要执行多次更新。
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);
});
供参考:
我有一个带有帐户的数据库。我需要将字段更新为每个帐户中的不同字段。 该帐户看起来像这样:
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);
})
但是当我尝试对数组执行相同操作时,控制台中出现错误:
TypeError: a.update 不是函数
我正在尝试这样做:
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 中有一个解决方法来批量更新数据,所以不需要执行多次更新。
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);
});
供参考: