Search code examples
node.jsloopbackjsstrongloop

Method on Extended User Model


I am trying to write a method on my extended user model, "Person" (plural: People), that lists all the e-mail addresses so that later, users can find their friends.

Right now this is what my Person.js file looks like:

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']
        }
    );

};

The remote method above was generated automatically when building out the relational model in this experimental app. I've read the documentation on how to create a remote method, but I've not found it helpful enough to extrapolate what I need to do here.

Just for now, I want to create a method called findEmailAddresses and have it return all the e-mails for all users. I don't see any examples in the docs of how to return an array in a remote method much less or create more than one remote method in a single model. Here was my attempt, I'm just guessing at this, but it doesn't show up in the explorer as an option like the getPrefs method does:

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']
        }
    );

};

Solution

    1. To create more than one remote method in a single model
    2. To create a method called findEmailAddresses and have it 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"
        }
      ],