Search code examples
node.jspromisefeathersjsfeathers-sequelizefeathers-hook

How to treat a promise inside a feathersjs hook?


I want to validate data before inserting on database. Feathersjs way is by using hooks. Before inserting a group of permissions, I must take in consideration the integrity of data provided by the user post. My solution is to find all the permissions associated to data provided by user. By comparing the lengths of the lists I can proove if data is right. The code of the hook is posted bellow:

const permissionModel = require('./../../models/user-group.model');

module.exports = function (options = {}) { 
  return function usergroupBefore(hook) {
    function fnCreateGroup(data, params) {
      let inIds = [];
      // the code in this block is for populating the inIds array

      if (inIds.length === 0) {
        throw Error('You must provide the permission List');
      }
      //now the use of a sequalize promise for searching a list of
      // objects associated to the above list
      permissionModel(hook.app).findAll({
         where: {
          id: {
            $in: inIds
          }
       }
      }).then(function (plist) {
        if (plist.length !== inIds.length) {
          throw Error('You must provide the permission List');
        } else {
          hook.data.inIds = inIds;
          return Promise.resolve(hook);
        }
      }, function (err) {
        throw err;
      });
    }

    return fnCreateGroup(hook.data);
  };
};

I commented the lines that process some information of others parameters for populate the inIds array. I also used a sequalize search for the objects associated to the information stored into the array.

This block inside the then block is executed in background. On the feathersjs console is shown the results

code execution

However, data was inserted into database.

How can I return data from a promise executed inside a feathersjs hook?


Solution

  • Your fnCreateGroup is not returning anything. You have to return permissionModel(hook.app).findAll. Alternatively if you are using Node 8+ async/await will make this a lot easier to follow:

    const permissionModel = require('./../../models/user-group.model');
    
    module.exports = function (options = {}) { 
      return async function usergroupBefore(hook) {
        let inIds = [];
        // the code in this block is for populating the inIds array
    
        if (inIds.length === 0) {
          throw Error('You must provide the permission List');
        }
    
        //now the use of a sequalize promise for searching a list of
        // objects associated to the above list
        const plist = await permissionModel(hook.app).findAll({
            where: {
            id: {
              $in: inIds
            }
          }
        });
    
        if (plist.length !== inIds.length) {
          throw Error('You must provide the permission List');
        } else {
          hook.data.inIds = inIds;
        }
    
        return hook;
      };
    };