我有一个如下的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;
}
但是我在插入/更新后没有收到控制台消息。有解决这个问题的主意吗?
规格如下
节点版本: v8.16.2
sequelize版本: sequelize@4.42.0
答案 0 :(得分:2)
您可以尝试:
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;