Javascript 如何在花括号内获取变量的值?
我需要forEach之外的Javascript 如何在花括号内获取变量的值?,javascript,typescript,Javascript,Typescript,我需要forEach之外的that.assigned值,但如果我将其用作console.log(that.assigned)数组,则会导致undefined assignedDoctors.then(function(doc){ let i = 0; doc.forEach(function(md){ tmpMDs.push(md.data()); tmpMDs[i].key = md.id; // tmpMDs.push(md.data().push());
that.assigned
值,但如果我将其用作console.log(that.assigned)
数组,则会导致undefined
assignedDoctors.then(function(doc){
let i = 0;
doc.forEach(function(md){
tmpMDs.push(md.data());
tmpMDs[i].key = md.id;
// tmpMDs.push(md.data().push());
i++;
});
that.assigned = tmpMDs;
}).catch(function(e){
console.log(e);
});
console.log(that.assigned)
assignedDoctors.then是一个承诺函数,在调用承诺函数之前不能访问该变量的值。assignedDoctors.then是一个承诺函数,在调用承诺函数之前不能访问该变量的值。如果使用ES 2017,则可以执行此操作
(async function(){
that.assigned = await assignedDoctors.then(function(doc){
let i = 0;
doc.forEach(function(md){
tmpMDs.push(md.data());
tmpMDs[i].key = md.id;
// tmpMDs.push(md.data().push());
i++;
});
return tmpMDs;
});
})()
否则,无法使用回调。如果您使用的是ES 2017,那么您可以这样做
(async function(){
that.assigned = await assignedDoctors.then(function(doc){
let i = 0;
doc.forEach(function(md){
tmpMDs.push(md.data());
tmpMDs[i].key = md.id;
// tmpMDs.push(md.data().push());
i++;
});
return tmpMDs;
});
})()
否则,无法使用回调。可能重复的可能重复的可能重复的我不能使用分配给异步外部的。不,你不能。这样javascript就可以工作了。你可以称之为javascript的缺点或优点。哦,好的,明白了。无论如何,谢谢。我不能用那个。不,你不能。这样javascript就可以工作了。你可以称之为javascript的缺点或优点。哦,好的,明白了。无论如何谢谢你