gpt4 book ai didi

javascript - 嵌套循环内上下文值未定义

转载 作者:行者123 更新时间:2023-12-03 01:20:28 29 4
gpt4 key购买 nike

下面的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

最佳答案

看起来 context.app.service('emplist').find() 调用是异步的。它会影响上下文。因此,可能的答案是 context.data 对象在 context.app.service('emplist').find() 工作期间被清理。

关于javascript - 嵌套循环内上下文值未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51790836/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com