【发布时间】:2015-11-20 01:23:58
【问题描述】:
我正在尝试在我的扩展用户模型“Person”(复数:People)上编写一个方法,该方法列出所有电子邮件地址,以便用户稍后可以找到他们的朋友。
现在这就是我的 Person.js 文件的样子:
module.exports = function(Person) {
Person.getPrefs = function(personId, cb) {
Person.findById(personId,{ include: [{ relation: 'foodPrefs', scope: { include: { relation: 'food_pref_to_food_type' }}}]}, function(err, personFound) {
if (err) {
return cb(err);
}
cb(null, personFound);
});
}
Person.remoteMethod(
'getPrefs', {
http: {path: '/:personId/getPrefs', verb: 'get'},
accepts: [{arg: 'personId', type: 'number'}],
returns: {arg: 'type', type: 'object'},
description: ['a person object']
}
);
};
上述远程方法是在此实验应用中构建关系模型时自动生成的。我已阅读有关如何创建远程方法的文档,但我发现它没有足够的帮助来推断我需要在这里做什么。
现在,我想创建一个名为 findEmailAddresses 的方法并让它返回所有用户的所有电子邮件。我在文档中没有看到任何关于如何在远程方法中返回数组或在单个模型中创建多个远程方法的示例。这是我的尝试,我只是在猜测,但它并没有像 getPrefs 方法那样作为选项显示在资源管理器中:
module.exports = function(Person) {
Person.getPrefs = function(personId, cb) {
Person.findById(personId,{ include: [{ relation: 'foodPrefs', scope: { include: { relation: 'food_pref_to_food_type' }}}]}, function(err, personFound) {
if (err) {
return cb(err);
}
cb(null, personFound);
});
}
Person.findEmailAddresses = function(cb) {
Person.find(function(err, peopleFound) {
if (err) {
return cb(err);
}
cb(null, peopleFound);
});
}
Person.remoteMethod(
'getPrefs', {
http: {path: '/:personId/getPrefs', verb: 'get'},
accepts: [{arg: 'personId', type: 'number'}],
returns: {arg: 'type', type: 'object'},
description: ['a person object']
},
'findEmailAddresses', {
http: {path: '/:Person', verb: 'get'},
returns: [{arg: 'email', type: 'object'}],
description: ['all emails']
}
);
};
【问题讨论】:
标签: node.js loopbackjs strongloop