0
我試圖讓2個遠程方法在同一個環回模型中工作,但只有一個工作在api資源管理器中。如果我評論/刪除其中一個的代碼,另一個完美地工作。下面是我如何實現它:環回,在同一模型中聲明2個遠程方法,但只能在api資源管理器中使用一個
module.exports = function(Usrs) {
var db = server.dataSources.wifiMongo;
db.connect(function(err, db) {
Usrs.authMethod = function(cb) {
db.collection('users', function(err, collection) {
if (err)
return console.log('Error al encontrar la collección, err = ', err);
collection.aggregate([{
"$group": {
_id: "$strategy",
count: {
$sum: 1
}
}
}, {
$project: {
tmp: {
label: '$_id',
value: '$count'
}
}
}, {
$group: {
_id: 'methods',
data: {
$addToSet: '$tmp'
}
}
}], function(err, methods) {
if (err) {
console.log('err = ' + err);
} else {
methods[0].data.forEach(function(method) {
method.color = randomColor({
luminosity: 'bright'
});
});
cb(null, methods[0].data);
}
});
});
};
});
Usrs.remoteMethod('authMethod', {
http: {
path: '/analytics/authMethod',
verb: 'get'
},
returns: {
arg: 'authMethod',
type: 'Object'
}
});
//-------------------------------------------------------------------------//
db.connect(function(err, db) {
if (err) return console.log('error al conectar wifiMongo, err = ', err);
Usrs.devices = function(cb) {
db.collection('clients', function(err, collection) {
if (err)
return console.log('Error al encontrar la collección, err = ', err);
collection.aggregate([{
"$group": {
_id: "$os",
count: {
$sum: 1
}
}
}], function(err, client) {
if (err) {
console.log(err);
} else {
var output = client.reduce(function(a, b) {
var match = b._id ? b._id.match(/(Mac|Windows|Android|iOS)/) : null;
match = match ? match[0] : 'Others';
a[match] = (a[match] || 0) + b.count;
return a;
}, {});
output = Object.keys(output).map(function(k) {
return {
label: k,
value: output[k],
color: randomColor({
luminosity: 'bright'
})
};
});
cb(null, output);
}
});
});
};
});
Usrs.remoteMethod('devices', {
http: {
path: '/analytics/devices',
verb: 'get'
},
returns: {
arg: 'devices',
type: 'Object'
}
});
};
如果在api資源管理器中嘗試它,第一個運行沒有錯誤。 另一個給我內部服務器錯誤。 的記錄以下:
Unhandled error for request GET /usrs/analytics/devices: TypeError: Cannot read property 'collection' of undefined
at Function.Usrs.devices (/home/ubuntu/loopback/common/models/usrs.js:72:6)
at SharedMethod.invoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/shared-method.js:262:25)
at HttpContext.invoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/http-context.js:295:12)
at phaseInvoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:647:9)
at runHandler (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5)
at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)
at Object.async.eachSeries (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9)
at runHandlers (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:144:13)
at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)
at /home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:157:25
at /home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
at execStack (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:492:7)
at RemoteObjects.execHooks (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:496:10)
at phaseBeforeInvoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:643:10)
at runHandler (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5)
at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)
我甚至已經試過將它們放置在不同的車型,但仍然出現同樣的錯誤。