繁体   English   中英

Uncaught (in promise) TypeError: snapshot.data is not a function 当尝试从 Firebase Firestore 检索文档时

[英]Uncaught (in promise) TypeError: snapshot.data is not a function when trying to retrieve documents from Firebase Firestore

我正在尝试检索集合中的所有文档,但我收到一个错误作为回报:Uncaught (in promise) TypeError: snapshot.data is not a function。

代码:

var imgRef = db.collection('posts').doc(uid).collection('userPosts');

        imgRef.get().then(function(snapshot) {

            const data = snapshot.data()

            console.log(data)

            if (data) {
                const picURL = data.picURL

                console.log(picURL)

                var img = document.querySelector(".img")

                img.src = picURL

            } else {

                console.log("PicURL doesn't exist")

            }

snapshot将是QuerySnapshot (不是DocumentSnapshot ),因为 API 不知道您正在处理多少文档。 QuerySnapshot 没有data()方法。 您必须单独处理每个文档数据。

var imgRef = db.collection('posts').doc(uid).collection('userPosts');
imgRef.get().then(function(snapshot) {
    if (snapshot.docs.length > 0) {
        snapshot.docs.forEach(doc => {
            // doc is a DocumentSnapshot with actual data
            const data = doc.data();
        })
    }
    else {
        // decide what you want to do if there are no documents returned from the query
    }
});

暂无
暂无

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

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