Paul Baiju
Paul Baiju

Reputation: 419

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) {})

    })

I have referred to other solutions which have said about returning promise, but i have not understood what that means.

Upvotes: 0

Views: 1328

Answers (2)

Ali Faris
Ali Faris

Reputation: 18592

forEach not returning Promise , it returns undefined implicitly

checkout this

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);
                });
            });

        })

    })

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074138

forEach doesn't return anything, so calling it always results in undefined.

You probably wanted map and 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) {})
    })

Upvotes: 3

Related Questions