我已经使用sequelize-auto生成了模型,并且需要使用beforeSave钩子(请参阅here https://stackoverflow.com/questions/47795113/insert-update-postgis-geometry-with-sequelize-orm)。据我所知,钩子还没有发射。 sequelize 版本 ^4.20.1,sequelize-auto 版本 ^0.4.29,express 版本 ~4.15.5。有人可以帮忙吗?
module.exports = function(sequelize, DataTypes) {
return sequelize.define('trad', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
geom: {
type: DataTypes.GEOMETRY('POINT', 4326),
allowNull: true
},
...
}, {
hooks: {
beforeSave: (instance, options) => {
console.log('Saving geom: ' + instance.geom);
if (instance.geom && !instance.geom.crs) {
instance.geom.crs = {
type: 'name',
properties: {
name: 'EPSG:4326'
}
};
}
}
},
tableName: 'trad',
timestamps: false,
});
};
以下是 PUT 请求的代码:
// Update (PUT)
router.put('/table/:table/:id', function(req, res, next) {
db.resolveTableName( req )
.then( table => {
const primaryKey = table.primaryKeyAttributes[0];
var where = {};
where[primaryKey] = req.params.id;
console.log('Put - pkey: ' + primaryKey);
auth.authMethodTable( req )
.then( function() {
table.update( req.body, {
where: where,
returning: true,
plain: true
})
.then( data => {
res.status(200).json( data[1].dataValues );
})
.catch( function (error ) {
res.status(500).json( error );
});
})
.catch( function( error ) {
res.status(401).json('Unauthorized');
});
})
.catch( function(e) {
res.status(400).json('Bad request');
});
});