gpt4 book ai didi

node.js - 如何控制环回中的插入操作?

转载 作者:搜寻专家 更新时间:2023-11-01 00:37:34 26 4
gpt4 key购买 nike

有什么方法可以控制或监视特定模型的环回插入操作并在插入调用函数后?例如:提交表单后 if 表单成功插入验证邮件发送到输入的邮件地址或短信到输入的电话号码

'use strict';

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

app.start = function() {
// start the web server
return app.listen(function() {
app.emit('started');
var baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
var explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
});
};

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
if (err) throw err;

// start the server if `$ node server.js`
if (require.main === module)
app.start();
});

var properties = {};
var options = {trackChanges: true };

var MyModel = loopback.Model.extend('Registration', properties, options);
MyModel.on('changeed', function(inst) {
console.log('model with phonenumber %s has been changed', inst.phonenumber);
});

最佳答案

如果你想要一个特定的模型,你应该使用 Operation hooks

你有“保存前”和“保存后”,你可以运行任何你想要的额外逻辑,比如发送电子邮件。

./server/my-mode.js

MyModel.observe('before save', function (ctx, next) {
if (ctx.instance) {
// When Create (POST)
// ctx.instance have the json properties
console.log("Triggers when create");
} else {
// When Update (UPDATE)
// ctx.data have the json properties
console.log("Triggers when update");
}
next();
});

./server/my-mode.js

MyModel.observe('after save', function(ctx, next) {
if (ctx.instance) {
// When Create (POST)
// ctx.instance have the json properties
console.log("Triggers after create");

} else {
// When Update (UPDATE)
// ctx.data have the json properties
console.log("Triggers after update");
}
next();
});

关于node.js - 如何控制环回中的插入操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127722/

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