gpt4 book ai didi

node.js - Sequelize 钩子(Hook)不触发

转载 作者:行者123 更新时间:2023-12-03 22:23:22 25 4
gpt4 key购买 nike

我有一个 Sequelize 模式如下:

import sequelize from "../config/sequelize-config";
import { INTEGER, FLOAT, DATE } from "sequelize";

var UserRatingSchema = sequelize.define(
'user_rating', {
id: {
type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true
},
user_id: { type: INTEGER, allowNull: false },
box_id: { type: INTEGER, allowNull: false },
product_id: { type: INTEGER, allowNull: false },
rating: { type: INTEGER, allowNull: false },
created_on: { type: DATE, allowNull: true },
updated_on: { type: DATE, allowNull: false }
},
{
indexes: [{
unique: false,
fields: ['id', 'user_id']
}],
classMethods: {},
timestamps: false
},
{
hooks: {
afterUpdate: function (user, options, fn) {
console.log("works after the update on user rating table")
},
afterCreate: function (user, options, fn) {
console.log("works after the create on user rating table")
}
}

});

export default UserRatingSchema;

更新数据如下:
import UserRatingSchema from "../db-schemas/user-rating";
rateUserExperience(params) {
let data = new Observable(observer => {
UserRatingSchema.update({
user_id: params.userId,
box_id: params.bookingId,
product_id: params.productId,
rating: params.rating,
created_on: Date.now(),
updated_on: Date.now()
},
{ where: { user_id: params.userId } }
)
console.log("Rating done successfully")
})
.catch(error => {
observer.error(error);
});
}
});
return data;
}

但是 插入/更新后我没有收到控制台消息 .有什么想法可以解决这个问题吗?

规范如下

Node 版本: v8.16.2

Sequelize 版本: Sequelize @4.42.0

最佳答案

你可以试试:

import sequelize from "../config/sequelize-config";
import { INTEGER, FLOAT, DATE } from "sequelize";

var UserRatingSchema = sequelize.define(
'user_rating', {
id: {
type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true
},
user_id: { type: INTEGER, allowNull: false },
box_id: { type: INTEGER, allowNull: false },
product_id: { type: INTEGER, allowNull: false },
rating: { type: INTEGER, allowNull: false },
created_on: { type: DATE, allowNull: true },
updated_on: { type: DATE, allowNull: false }
},
{
indexes: [{
unique: false,
fields: ['id', 'user_id']
}],
classMethods: {},
timestamps: false,
hooks: {
afterUpdate: function (user, options, fn) {
console.log("works after the update on user rating table")
},
afterCreate: function (user, options, fn) {
console.log("works after the create on user rating table")
}
}

});

export default UserRatingSchema;

关于node.js - Sequelize 钩子(Hook)不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com