Loopbackjs 回送奇怪的行为
我说的是环回推送组件。我试图拦截“安装”模型的“创建”方法。我的代码如下所示- server/boot/installationex.jsLoopbackjs 回送奇怪的行为,loopbackjs,strongloop,Loopbackjs,Strongloop,我说的是环回推送组件。我试图拦截“安装”模型的“创建”方法。我的代码如下所示- server/boot/installationex.js module.exports = function (app) { var Installation = app.models.Installation; var create = Installation.create; Installation.create = function (data, cb) { //reinitializing old
module.exports = function (app) {
var Installation = app.models.Installation;
var create = Installation.create;
Installation.create = function (data, cb) {
//reinitializing old implementation
this.create = create;
console.log("Received data: "+JSON.stringify(data));
if (!data || !data.imei) {
console.log("No data or imei was provided, creating new");
this.create(data, cb);
return;
}
//saving 'this' reference
var that = this;
//search by imei filter
var filter = {where: {imei: data.imei}};
this.findOne(filter, function (err, result) {
if (err) {
console.log("Error occurred while looking for installation by IMEI");
cb(err);
return;
}
if (!result) {
console.log("No installation found by IMEI, will create a new installation");
that.create(data, cb);
return;
}
console.log("Found existing installation with id: " + JSON.stringify(result));
result.deviceToken = result.gpsLocation = result.osVersion = result.vendor = result.phoneNumbers = null;
if (data.deviceToken) {
result.deviceToken = data.deviceToken;
}
if (data.gpsLocation) {
result.gpsLocation = data.gpsLocation;
}
if (data.osVersion) {
result.osVersion = data.osVersion;
}
if (data.vendor) {
//result.vendor=data.vendor;
result.vendor = 'jahid';
}
if (data.phoneNumbers) {
result.phoneNumbers = data.phoneNumbers;
}
that.upsert(result, cb);
});
}
}
不幸的是,这段代码只被调用一次,我是说第一次。此后,此代码将永远不会被调用。我看了看日志就知道了。它只会第一次打印日志。之后,它不会打印任何日志
知道为什么这个粘合代码只被调用一次吗?我的目的是拦截安装模型的所有create方法调用。并检查是否已经有提供的“IMEI”条目,如果是,则重用该条目。否则,创建新的
提前谢谢
致以最良好的祝愿
贾希德我首先要说的是:
启动脚本在应用程序启动期间只运行一次。如果希望函数在每次调用时触发,请使用远程挂钩或模型挂钩。大概是这样的:
...
Installation.beforeRemote('create', ...
...
有关更多信息,请参见Hi,根据此链接(在页面底部),这是重载内置方法的建议方法。有任何示例说明如何从Android端调用“findOrCreate()”?我的意思是不是服务器端,但我需要客户端代码。