簡體   English   中英

TypeError:無法讀取未定義的屬性“ then”(返回承諾?)

[英]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

您可能想要mapPromise.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM