Search code examples
javascriptjavascript-objectsfeathersjsfeathers-hook

Context value getting undefined inside nested Loop


Below java-script code context value became undefined inside nested loop.but outside the nested loop the values is properly showing.

please help to show the context value inside the loop.

module.exports = function (options = {}) { 
  return async context => {
    const { data } = context;

     context.app.service('emplist').find({
        query: { empId: { $in: ["12321"] } },
        paginate: false,
    }).then(result => {

        console.log('firstname_Inside-->',context.data.firstname);

    }).catch((error) => {
          console.log(error);
    });

        console.log('firstname_Outside-->',context.data.firstname);

    return context;
  };
};

OUTPUT:-

//here value is undefined
firstname_Inside-->undefined

//here value is properly showing
firstname_Outside-->sam

Solution

  • Looks like context.app.service('emplist').find() call is asynchronous. And it affects the context. So the possible answer would be that context.data object is being cleaned up during context.app.service('emplist').find() work.