我有以下代码:
myTable()
.update(data, {
where: criteria
})
.then(delay(100))
.then((entries) => {
...
...
.then(delay(100))
部分将延迟设置为100ms。
如果我不使用该延迟,则有时entries
(更新后的结果行)不正确,这意味着它们的字段未更新。但是有时候是。
如果我使用延迟,则entries
的内容总是正确的。
所以我的问题是:这是怎么回事?为什么必须设置延迟才能使其正常工作?
编辑:
我的本地MySQL my.cnf
文件:
[mysqld]
general_log_file = /var/log/mysql.log
general_log = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
innodb_file_per_table = 1
innodb_log_file_size = 100M
interactive_timeout = 32000
lock_wait_timeout = 41536000
net_read_timeout = 120
net_write_timeout = 900
wait_timeout = 32000
max_allowed_packet = 1G
innodb_buffer_pool_size = 1G
关于表架构和模型:
它具有几个double
列,一对datetime
和char
,一个json
列和一个enum
列。
它们在模型中的定义相同。
答案 0 :(得分:0)
答案 1 :(得分:0)
因此,.then(...
块是在解决了更新承诺后执行的。并且它对更新查询没有影响。
return sequelize.transaction(function (t) {
// chain all your queries here. make sure you return them.
return yourModel.update(updates,{where: { 'id': id } },
{transaction: t}).then(function (entries) {
// your logic with your entries.
});
}).then(function (result) {
// Transaction has been committed
cb(null,result);
console.log('transaction commited');
}).catch(function (err) {
// Transaction has been rolled back
cb(err,null);
console.log('Transaction rolled back');
});
如果您对添加.then(delay(100))...
语句时发生的事情感到好奇,请使用catch
块,因为当您发现条目diffrenet与您所期望的不同时,这是因为查询已经无法更新