【问题标题】:Method on Extended User Model扩展用户模型方法
【发布时间】: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


    【解决方案1】:
    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"
        }
      ],
    

    【讨论】:

    • 这看起来确实可行,但由于某种原因,它仍然没有出现在资源管理器的方法列表中。
    • @NathanMcKaskle 我已经编辑了答案。现在它将显示在资源管理器中。您需要设置ACL 属性。
    • 是的,不幸的是,这似乎也没有奏效。我想可能也是这样。很奇怪。
    • @NathanMcKaskle 对不起.. 正确的语法应该是 returns: { arg: 'email', type: 'array' } 而不是 returns: [{ arg: 'email', type: 'array' }]
    • 它也可以是路径吗?也许这就是路径。当我将路径更改为 /Person/findEmailAddresses 时,它终于出现在资源管理器中,但它不能正常工作,所以我们至少要走得更远一些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2011-02-28
    • 2013-12-06
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多