Loopbackjs 如何在另一个持久化模型的另一个远程方法中调用一个持久化模型的远程方法
以下是我在model1.js中尝试的内容:Loopbackjs 如何在另一个持久化模型的另一个远程方法中调用一个持久化模型的远程方法,loopbackjs,strongloop,loopback,Loopbackjs,Strongloop,Loopback,以下是我在model1.js中尝试的内容: model1.remotemethod1 = function(id, data, cb) { var model2 = app.models.model2; model2.remotemethod2(id, data).then(response => { cb(null, true); }); }; 这是我的model2.js: 它具有remotemethod2的定义 'use strict'; module.expo
model1.remotemethod1 = function(id, data, cb) {
var model2 = app.models.model2;
model2.remotemethod2(id, data).then(response => {
cb(null, true);
});
};
这是我的model2.js:
它具有remotemethod2的定义
'use strict';
module.exports = function(model2) {
model2.remotemethod2 = function(id, data, cb) {
var promise;
let tags = data.tags ? data.tags.slice() : [];
delete data.categories;
delete data.tags;
promise = model2.upsertWithWhere({
or: [
{barcode: data.barcode},
{id: data.id},
],
}, data);
promise.then(function(model2) {
model2.tags.destroyAll().then(function() {
for (let i = 0; i < tags.length; i++) {
model2.tags.add(tags[i]);
}
cb(null, model2);
});
});
};
};
“严格使用”;
module.exports=函数(model2){
model2.remotemethod2=函数(id、数据、cb){
var承诺;
让tags=data.tags?data.tags.slice():[];
删除数据类别;
删除数据标签;
promise=model2.upsertWithWhere({
或:[
{barcode:data.barcode},
{id:data.id},
],
},数据);
promise.then(函数(模型2){
model2.tags.destroyAll()。然后(函数(){
for(设i=0;i
但是它不起作用!
我认为app.models.model2并没有给我提供带有远程方法的模型!也许我应该得到一个model2的实例 在
server.js
app.start
中声明remotemethod1
,您将可以访问正确的app.models.model2
,并可以使用其远程方法
app.start = function() {
model1.remotemethod1 = (id, data, cb) => {
var model2 = app.models.model2;
model2.remotemethod2(id, data).then(response => {
cb(null, true);
});
};
model1.remoteMethod(
'remotemethod1', {
http: { path: '/remotemethod1', verb: 'post', status: 200, errorStatus: 400 },
accepts: [{arg: 'id', type: 'number'}, {arg: 'id', type: 'object'}],
returns: {arg: 'status', type : 'string' }
}) ;
}
// The rest of app.start...
编辑您还可以使用位于myprojectname/server/boot
`module.exports(app) {
/* Create remote methods here */
}`
在
server.js
app.start
中声明remotemethod1
,您将可以访问正确的app.models.model2
,并可以使用其远程方法
app.start = function() {
model1.remotemethod1 = (id, data, cb) => {
var model2 = app.models.model2;
model2.remotemethod2(id, data).then(response => {
cb(null, true);
});
};
model1.remoteMethod(
'remotemethod1', {
http: { path: '/remotemethod1', verb: 'post', status: 200, errorStatus: 400 },
accepts: [{arg: 'id', type: 'number'}, {arg: 'id', type: 'object'}],
returns: {arg: 'status', type : 'string' }
}) ;
}
// The rest of app.start...
编辑您还可以使用位于myprojectname/server/boot
`module.exports(app) {
/* Create remote methods here */
}`
例如,您现在可以通过调用另一个模型的一个方法来使用它
例:
例如,您现在可以通过调用另一个模型的一个方法来使用它
例:
你能分享你的错误信息和远程方法的代码吗?我刚刚分享了remotemethod1代码!第二个不重要!错误消息是:“model2.remotemethod2不退出”您确定远程方法存在吗,可以在环回资源管理器中看到它吗?是的,它存在(即使我不需要它)。。我只想从另一个远程方法调用它。我想我的语法错了!主要问题是如何在模型外部调用远程方法。使用model2.js会更容易。你用正确的方式称呼它,但它显然不存在。你也在传递一个回调,同时期待一个承诺,这有点奇怪。你能分享你的错误消息和远程方法的代码吗?我刚刚分享了remotemethod1代码!第二个不重要!错误消息是:“model2.remotemethod2不退出”您确定远程方法存在吗,可以在环回资源管理器中看到它吗?是的,它存在(即使我不需要它)。。我只想从另一个远程方法调用它。我想我的语法错了!主要问题是如何在模型外部调用远程方法。使用model2.js会更容易。你用正确的方式称呼它,但它显然不存在。您还传递了一个回调,同时希望得到一个承诺,这有点奇怪,我本可以在第一个remotemethod1中完成remotemethod2的工作!我只是想在model2.js中完成(编写)属于model2的代码!因此,将remotemethod1带到server.js对我来说与在model1中编写所有代码是一样的!我同意这并不理想,但它确实回答了问题。我本可以在第一个remotemethod1中完成remotemethod2的工作!我只是想在model2.js中完成(编写)属于model2的代码!因此,将remotemethod1带到server.js对我来说与在model1中编写所有代码是一样的!我同意这并不理想,但它确实回答了问题。