Sequelize.js 续集更新、创建挂钩后和文字日期

Sequelize.js 续集更新、创建挂钩后和文字日期,sequelize.js,Sequelize.js,在Sequelize中,我有一个模型WorkFile,它具有以下实例方法 workfile.update({ path: '/path/to/here' }).then(result => { return result; }) 这非常有效,除了结果包含以下内容 { "created_at": "2017-03-02T16:32:42.542Z", "updated_at": { "val": "NOW()" }, // etc } update方法起作用了

在Sequelize中,我有一个模型
WorkFile
,它具有以下实例方法

workfile.update({ path: '/path/to/here' }).then(result => {
  return result;
})
这非常有效,除了
结果
包含以下内容

{
  "created_at": "2017-03-02T16:32:42.542Z",
  "updated_at": {
    "val": "NOW()"
  },
  // etc
}
update方法起作用了,但是处的
updated_仍然是db文本只有在模型中有
afterCreate
钩子时才会发生这种情况。
我尝试了
workfile.get()
workfile.get({plain:true})
。我唯一能做的就是重新查询数据库中的对象,这看起来很可笑。有没有一种方法可以在没有文字更新的情况下访问对象


已编辑

这是钩子:

afterCreate: function(workfile, options, cb) {
    return workfile.update({
            path: `path/with/${workfile.id}`
        })
        .then(wf => cb(null, workfile)) // <--workfile and wf both have literal updated_at
        .catch(err => cb(err)); 
}
afterCreate:函数(工作文件、选项、cb){
返回workfile.update({
路径:`path/with/${workfile.id}`
})
.then(wf=>cb(null,工作文件))//cb(err));
}
您正在调用的函数将返回一个数组,其中0位置的更改行数和1位置的受影响行数。您看到的是
模型
而不是
实例
,这就是为什么会看到“NOW()”——它将在数据库和每个实例中转换为日期时间


afterCreate
函数中,您正在接受一个
分支
实例,然后在
工作文件
模型上调用
update()
而不使用
其中
,因此它将更新
工作文件
模型表中的所有行。您还可以得到一个数组,而不是上面提到的实例

不清楚
分支
是否为
工作文件
,但如果要更改该特定实例/行的属性,应使用:

// change the "path" on the "branch" Instance after each create
afterCreate: function(instance) {
  // "instance" is an Instance of a "workfile" Model
  instance.path: `path/with/${instance.id}`;
  // call Instance.save() on the "instance" variable it after you set the property.
  return instance.save();
}
如果您确实想更新所有
工作文件
s,而不管
分支是什么,您可以使用以下命令

// change the "path" on ALL "workfile" rows, displaying the results.
afterCreate: function() {
  return workfile.update({ path: `path/with/${workfile.id}` })
  .then(result => {
    console.log(`Updated ${result[0]} rows`);
    console.log('Affected rows:', result[1]);

    // since we are printing out the result here we also
    // need to return in in a Promise
    return Promise.resolve(result);
  })
  .catch(err => console.log("Error!", err));
}
您不需要包含未使用的函数参数,默认情况下,Sequelize使用承诺,因此您只需返回
update()
/
save()
,而不用回调

另一种选择是使用
VIRTUAL
字段,而不是将其存储在数据库中,因为您真正需要的是
ID

Sequelize.define('model', {
  path: {
    type: DataTypes.VIRTUAL,
    get: function getType() {
      return `path/with/${this.id}`;
    },
  }
);

对于未来的用户,在我的模型中,我使用了
Sequelize.literal('NOW()')
。当我用Sequelize.NOW
替换它时,问题就消失了。不太清楚为什么,但它可以解决问题

在这种情况下,确切的流程是什么?首先是
create
,然后是
afterCreate
钩子,然后是实例方法?你在哪个阶段得到安慰的结果?“钩子里发生了什么?”piotrbienias刚刚添加了afterCreate钩子。希望这能说明您使用的是哪台数据库服务器?您的
afterCreate
钩子用于
分支
,但您只是调用
workfile.update()
,而没有
WHERE
,因此它将更新所有内容。插入后,
NOW()
将转换为日期时间,
update()
函数返回的不是
实例
。抱歉,只是再次更新了它!这是工作文件模型,afterCreate在该模型上。我复制和粘贴错误的分支!谢谢你们让我直截了当对不起,doublesharp。布兰奇用词不当。我已经更新了这个问题,因为它确实是工作文件模型的一个实例。因此,更新是在一个实例上进行的。我在这里看到了一些想法,不过我会尝试一下~K,所以我已经切换到save()语法(我的after create看起来几乎和你的完全一样),我仍然会在实例上得到文本“NOW”作为返回值。你没有得到这样的东西吗?你能在问题中也包括
WorkFile
模型定义吗?你的模型名为
WorkFile
,实例名为
WorkFile
Model.update()
是一件事,而
Instance.update()
不是。查看第一个示例。如果您想获得更多乐趣,请在创建过程中使用
事务
,然后从钩子中的
选项访问它。这样,如果
路径
列的更新失败,您可以将其全部备份。但是,这可能不需要存储在数据库中,您可以使用
虚拟
字段来构造
路径