我正在尝试在我的扩展用户模型上编写一个方法," Person" (复数:人物),列出所有电子邮件地址,以便以后用户可以找到他们的朋友。
现在这是我的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的方法,让它返回所有用户的所有电子邮件。我没有在文档中看到如何以更少的方式返回远程方法中的数组或在单个模型中创建多个远程方法的示例。这是我的尝试,我只是在猜测,但它并没有像资源管理器方法那样出现在资源管理器中:
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']
}
);
};
答案 0 :(得分:1)
more than one
远程方法创建一个名为findEmailAddresses
的方法并将其return all the emails for all users
。
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']
}
);
//Now registering the method for returning all the email address of users.
Person.remoteMethod(
'findEmailAddresses', {
http: {
path: '/:Person',
verb: 'get'
},
returns: {
arg: 'email',
type: 'array'
},
description: ['all emails']
}
);
}
模型/ person.json
....
....
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "getPrefs"
},
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "findEmailAddresses"
}
],