![](/img/trans.png)
[英]React Promise - TypeError: Cannot read property 'then' of undefined
[英]TypeError: Cannot read property 'then' of undefined (return promise?)
firestore.collection("products").where("OrderNo", "==", inputx)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
var Nameout = doc.get("Name");
var path = 'products/' + inputx + '-' + Nameout;
tangRef = storageRef.child(path);
firebase.auth().signInAnonymously().then(function() {
tangRef.getDownloadURL().then(function(url) {
document.querySelector('img1').src = url;
}).catch(function(error) {
console.error(error);
});
});
}).then(function() {}).catch(function(error) {})
})
我已經提到過其他有關兌現承諾的解決方案,但是我不明白這意味着什么。
forEach
不返回Promise
,它隱式返回undefined
簽出這個
firestore.collection("products").where("OrderNo", "==", inputx)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc)
{
var Nameout = doc.get("Name");
var path = 'products/' + inputx + '-' + Nameout;
tangRef = storageRef.child(path);
firebase.auth().signInAnonymously().then(function () {
tangRef.getDownloadURL().then(function (url) {
document.querySelector('img1').src = url;
}).catch(function (error) {
console.error(error);
});
});
})
})
forEach
不返回任何內容,因此調用它總是導致undefined
。
您可能想要map
和Promise.all
:
firestore.collection("products").where("OrderNo", "==", inputx)
.get()
.then(function(querySnapshot) {
Promise.all(querySnapshot.map(function(doc) { // ***
var Nameout = doc.get("Name");
var path = 'products/' + inputx + '-' + Nameout;
tangRef = storageRef.child(path);
return firebase.auth().signInAnonymously().then(function() { // ***
tangRef.getDownloadURL().then(function(url) {
document.querySelector('img1').src = url;
}).catch(function(error) {
console.error(error);
});
});
})) // ***
.then(function() {})
.catch(function(error) {})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.