LOOPBACK 在钩子内修改另一个模型
LOOPBACK Modify another model inside a hook
当事务模型中的属性"code"等于3
时,我想在事件模型中插入新数据
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
//How to access and insert data in Event model ?
}
});
};
如果您的事件模型具有插入新数据的功能。仅参考模型。例如:
var eventModel = require('pathtoEventModel');
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
eventModel.saveNewData(ctxBeingNewData,function(response,next){
//manipulate as per your wish.
})
}
});
};
这对我有用
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
var eventModel = Transaction.app.models.Event;
eventModel.create({"nom":"ZONA ABC"}, function(err, obj){
//console.log('eventModel create', err);
});
}
});
};
这对我有用。就像@Sunil Lama 所说的,但我另外使用 loopback.getModel
server.js
const loopback = require('loopback');
const ModelInst = require('./model-instnace');
boot(app, __dirname, function (err) {
if (err) throw err;
ModelInst.event = (loopback.getModel('event'));
};
型号-instance.js
const ModelInst = {
...
event:null
...
}
model.js
const models = require('model-instnace Path');
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
models.event.insertNewDate('blah');
}
});
};
当事务模型中的属性"code"等于3
时,我想在事件模型中插入新数据 module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
//How to access and insert data in Event model ?
}
});
};
如果您的事件模型具有插入新数据的功能。仅参考模型。例如:
var eventModel = require('pathtoEventModel');
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
eventModel.saveNewData(ctxBeingNewData,function(response,next){
//manipulate as per your wish.
})
}
}); };
这对我有用
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
var eventModel = Transaction.app.models.Event;
eventModel.create({"nom":"ZONA ABC"}, function(err, obj){
//console.log('eventModel create', err);
});
}
});
};
这对我有用。就像@Sunil Lama 所说的,但我另外使用 loopback.getModel
server.js
const loopback = require('loopback');
const ModelInst = require('./model-instnace');
boot(app, __dirname, function (err) {
if (err) throw err;
ModelInst.event = (loopback.getModel('event'));
};
型号-instance.js
const ModelInst = {
...
event:null
...
}
model.js
const models = require('model-instnace Path');
module.exports = function(Transaction) {
Transaction.observe('before save', function(ctx, next) {
if(ctx.data.code == 3){
models.event.insertNewDate('blah');
}
});
};