루프백 푸시 구성 요소에 대해 이야기하고 있습니다. "설치"모델의 "작성"방법을 가로 채려고합니다. 내 코드는 다음과 같습니다 -루프백 이상한 동작
서버/부팅/I 처음을 의미,
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);
});
}
}
불행하게도이 코드는 한 번만 호출됩니다 installationex.js. 그 후이 코드는 호출되지 않습니다. 로그를보고 확신하게되었습니다. 처음으로 로그 만 인쇄합니다. 그 후에는 로그를 인쇄하지 않습니다.
이 접착제 코드가 한 번만 호출되는 이유는 무엇입니까? 내 의도는 설치 모델에 대한 모든 create 메소드 호출을 인터셉트하는 것입니다. 제공된 "IMEI"에 대한 항목이 이미 있는지 확인한 다음 다시 사용하십시오. 그렇지 않으면 새로 작성하십시오.
미리 감사드립니다.
안부,
Jahid
내가 호출 할 수있는 방법을 모든 예 "findOrCreate()"에 대한 http://docs.strongloop.com/display/LB/Adding+logic+to+models를 참조? 서버 측이 아니라 클라이언트 측 코드가 필요합니다. –