简体   繁体   English

我如何访问 map 之外的变量值?

[英]how i can access the variable value outside the map?

i want to access the variable outside the map with value but the value outside the map in null here is my code我想用值访问 map 之外的变量,但 null 中 map 之外的值是我的代码

let currentUser = await User.findById(req.user._id);
  let wordDetails = [];
  let findSameWord;
  let wordDetail = [];

  currentUser.learn.map(
    catchAsync(async (words) => {
       let wordDetail = await Learn.findById(words);
       wordDetails.push(wordDetail);  
       console.log(wordDetails); //here get me correct array
       return wordDetails;
     
    })
  );

  console.log(wordDetails); // here is an empty array

Try with that:试试看:

let currentUser = await User.findById(req.user._id);
let wordDetails = [];
let findSameWord;
let wordDetail = [];

await Promise.all(currentUser.learn.map(
    catchAsync(async (words) => {
        let wordDetail = await Learn.findById(words);
        wordDetails.push(wordDetail);
        console.log(wordDetails); //here get me correct array
        return wordDetails;

    })
));

console.log(wordDetails); // here is an empty array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM