【问题标题】:Firestore : get all files ( only specify ids)Firestore:获取所有文件(仅指定 id)
【发布时间】:2021-10-14 11:44:07
【问题描述】:

我需要检索所有具有指定 id 的文件

我的火库(结构)

users/user1/img1 -> [ fileId = img1  , file = img1.jpg ]
users/user2/img2 -> [ fileId = img2  , file = img2.jpg ]
users/user3/img3 -> [ fileId = img3  , file = img3.jpg ]

我在文档中找到了“getAll”方法(@​​987654321@)

但是以他们的例子,它不起作用。

我的代码

import storage  from "@react-native-firebase/storage";

let docRef1 = storage().doc('/users/user1/img1');
let docRef2 = storage().doc('/users/user2/img2');
return new Promise((resolve, reject) => {
    // Now we get the references of these images
    storage().getAll(docRef1, docRef2).then(docs => {
        resolve()
    }).catch(function(error) {
        // Handle any errors
        console.log('error', error)
        reject()
    });

我的错误:

TypeError: (0, _storage.default)().doc is not a function. (In '(0, _storage.default)().doc('/products/3799569030005-1.jpg')', '(0, _storage.default)().doc' is undefined)

更新 1:在 Frank van Puffelen 的帖子之后 我修改我的代码以使用 id 进行测试

import firestore from "@react-native-firebase/firestore";
let docRef1 = firestore().collection('users').doc('user1/img1');
let docRef2 = firestore().doc('/users/user1/img1');
// use with docRef1  or docRef2
docRef2.get().then(docSnapshots => {
        console.log('AA1 docSnapshots data', docSnapshots.data())
    }).catch(err => {
        console.log('AA2 err', err)
    })
// error for the docRef1 and docref2 =>  Error: firebase.firestore().doc(*) 'documentPath' must point to a document.

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    通过调用storage(),您正在使用用于 Firebase Storage 的 API,该 API 可以访问 Cloud Storage 中的文件(存储文件)。但是你说你想访问 Cloud Firestore 中的文档,这是一个 noSQL 文档数据库。

    虽然 Firestore 和 Storage 都可以通过 Firebase SDK 访问,但它们是完全独立的,其中一个的 API 与另一个无关。如果您想访问 Firestore,请使用 firebase.firestore()...(或在 react-native-firebase 上使用 firestore())。


    您链接到的 getAll() 方法存在于 Node.js SDK for Firestore 中。它不存在于 JavaScript/Web SDK 和 react-native-firebase 包装器中。在这些 SDK 中,您必须在每个单独的 DocumentReference 对象上调用 get,或者您可以在 FieldPath.documentId() 上使用 in query

    第一种方法(因为它最接近您问题中的代码):

    let docRef1 = firestore().doc('/users/user1/img1');
    let docRef2 = firestore().doc('/users/user2/img2');
    Promise.all([docRef1.get(), docRef2.get()])
      .then((docSnapshots) => {
        console.log(docSnapshots[0].data(), docSnapshots[1].data());
      });
    

    【讨论】:

    • 您好,感谢您的回复。我测试了你的例子,但它对我不起作用。我更新了我的帖子
    • 这是一个不同的问题:您的firestore().collection('users').doc('user1/img1'); 指向顶级集合users,该集合中的user 文档,然后是该文档中的img1 子集合。所以你需要使用collection 调用它。
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多