0

I'm new to FeathersJS and want to add additional data to the user object after authorization. (I want to access the data with app.get('user').newData)

First I tried to set a value in hook.result.newData, but it doesn't work. I'm only able to change an existing value like email.

module.exports = function(options) {
  return function(hook) {
    hook.result.households = hook.app.service('households').find({'people.userId': hook.id}).then(household => {
      console.log('households: ', household.data);
      return household.data;
    });
    hook.result.email = '!' + hook.result.email + '?'; // this works
    hook.result.newData = 'test'; // this doesn't work
  };
};

Another approach I tried was modifying the code snippet found here: https://github.com/feathersjs/feathers-hooks/issues/56 but also with no success.

But I'm also not sure how to populate in my case. I want to add objects of another service, which have an array of user ids. And I only want those objects, which contain the own user id among others in the array.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
tan
  • 51
  • 1
  • 8
  • 1
    If you are using Mongoose, you have to convert the models to a plain JavaScript object first in order to change fields. See http://docs.feathersjs.com/databases/mongoose.html#modifying-results-with-the-toobject-hook – Daff Aug 20 '16 at 08:37
  • Thanks! Adding fields is working now. But I still can't create a new field based on a .find() call, I know that it is an asynchronous call, but I don't know how to set the value properly. Do you know a way I could implement my plan with a populate? The challenge for me is, that the user id is an array value inside a nested object. It has to be something like this: `const populateHouseholds = hooks.populate('households', { service: 'households', field: 'people.userId', fieldName: '_id' });` – tan Aug 21 '16 at 08:55
  • 1
    The built in hook does not support nested fields. The next version will have this one that does: https://gist.github.com/daffl/2512fd29b8159f03843653fc17997ab7 – Daff Aug 21 '16 at 10:35

0 Answers0