嵌套循环中上下文值变得不确定

时间:2018-08-10 16:45:48

标签: javascript javascript-objects feathersjs feathers-hook

在嵌套循环内Java脚本代码上下文值以下未定义,但在嵌套循环外该值正确显示。

请帮助显示循环内的上下文值。

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;
  };
};

输出:-

//here value is undefined
firstname_Inside-->undefined

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

1 个答案:

答案 0 :(得分:1)

类似context.app.service('emplist').find()的调用是异步的。它会影响context。因此可能的答案是context.data工作期间正在清理context.app.service('emplist').find()对象。

相关问题