扩展用户模型方法

Method on Extended User Model

我正在尝试在我的扩展用户模型 "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 的方法,并用它 return 所有用户的所有电子邮件。我没有在文档中看到任何关于如何 return 远程方法中的数组更少或在单个模型中创建多个远程方法的示例。这是我的尝试,我只是在猜测,但它并没有像 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']
        }
    );

};
  1. 在单个模型中创建 more than one 远程方法
  2. 创建一个名为 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']
        }
    );
    }
    

models/person.json

 ....
 ....

 "acls": [

    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "getPrefs"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "findEmailAddresses"
    }
  ],