gpt4 book ai didi

node.js - LOOPBACK 在钩子(Hook)内修改另一个模型

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:25 24 4
gpt4 key购买 nike

当事务模型中的属性“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 ?
}
});
};

最佳答案

这对我有用。就像@Sunil Lama 所说的那样,但我另外使用了loopback.getModel

服务器.js

const loopback = require('loopback');
const ModelInst = require('./model-instnace');
boot(app, __dirname, function (err) {
if (err) throw err;
ModelInst.event = (loopback.getModel('event'));
};

模型实例.js

const ModelInst = {
...
event:null
...
}

模型.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');
}
});
};

关于node.js - LOOPBACK 在钩子(Hook)内修改另一个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47343277/

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