【问题标题】:React native problem saving data to firestore反应本机问题,将数据保存到 Firestore
【发布时间】:2020-07-20 01:22:03
【问题描述】:

这是我的代码。

import FirebaseKeys from "./config";
import firebase from 'firebase/app';

class Fire {
    constructor() {
        firebase.initializeApp(FirebaseKeys);
    }

   addPost = async ({text, localUri}) => {
       const remoteUri = await this.uploadPhotoAsync(localUri)

       return new Promise((res, rej) => {
           this.firestore.collection("posts").add({
               text,
               uid: this.uid,
               timestamp: this.timestamp,
               image: remoteUri
           })
           .then(ref => {
               res(ref)
           })
           .catch(error => {
               rej(error)
           });
       });
   };

    uploadPhotoAsync = async uri => {
        const path = `photos/${this.uid}/${Date.now()}.jpg`

        return new Promise(async (res, rej) => {
            const response = await fetch(uri)
            const file = await response.blob()

            let upload = firebase.storage().ref(path).put(file)

            upload.on(
                "state_changed",
                 snapshot => {},
                err => {
                     rej(err)
            },
            async () => {
                const url = await upload.snapshot.ref.getDownloadURL();
                res(url);

                }
            );
        });
    };

我注意到在 uploadImageAsync 上上传图片没有问题,但是当涉及到 addPost 方法时,它不会创建一个名为“post”的集合。

我该怎么办?先感谢您。顺便说一句,我没有在这上面使用 expo。

【问题讨论】:

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


    【解决方案1】:

    首先,您的addPost 方法尝试创建的集合称为“posts”,而不是“post”。对于 Firestore,请始终检查您的安全规则是否允许该用户写入该路径。

    也很高兴看到addPost 的用法,看看它的resrej 返回什么。

    【讨论】:

    • 你好,最近我也注意到使用addpost方法的一些事务是成功的数据正在显示,但大多数时候它并不适用于数据库
    • 您能否展示您为此集合配置的安全规则,以及addPost 方法的用法?
    • rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { 允许读取,写入:如果为真; } } } 这是先生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多