【问题标题】:Unable to add a Record into Firebase upon creating User in React Native Expo在 React Native Expo 中创建用户时无法将记录添加到 Firebase
【发布时间】:2021-02-15 15:14:49
【问题描述】:

所以我尝试在 Firebase 中使用用于 Auth 的 Rest API 创建一个用户,但在我在 Auth 中创建用户并取回用户 UID 后,我需要再做 2 件事:

  1. 将图片上传到 Firebase 存储。

  2. 在实时数据库中的用户集合(表)中创建用户记录。也使用 Rest API。 Firebase 自动化 SDK 并不适合我。

第一件事,(图片上传)我可以通过我的代码来解决我遇到的问题是将记录插入到数据库中的集合中。

这是我目前的注册码:

export const signup = (email, password, image, nombre) => {
    return async dispatch => {
        const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[my API KEY]', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                email: email,
                password: password,
                returnSecureToken: true,
            })
        });
        if (!response.ok) {
            const errorResData = await response.json();
            const errorId = errorResData.error.message;
            let message = 'Algo salio mal!';
            if (errorId === 'EMAIL_EXISTS') {
                message = 'Este Correo ya esta en Uso';
            }
            throw new Error(message);
        }

        const resData = await response.json();

        const nameImage = resData.localId + '.jpg';
        
        const imageResponse = await fetch(image);
        const blob = await imageResponse.blob();

        var ref = firebase.storage().ref().child("images/"+nameImage);

        await ref.put(blob);

        //la variable url tiene la url de la imagen que se guardara en la tabla usuario
        const url = await ref.getDownloadURL().catch((err) => {console.log(err)});

        writeUser(resData.localId, email, nombre, url); 


        // dispatch(
        //     authenticate(
        //         resData.localId,
        //         resData.idToken,
        //         parseInt(resData.expiresIn) * 1000
        //     )
        // );
        // const expirationDate = new Date(new Date().getTime() + parseInt(resData.expiresIn) * 1000);
        // saveDataToStorage(resData.idToken, resData.localId, expirationDate);
    };
};

这里是写用户函数:

export const writeUser = async (id, email, nombre, foto) => {
    const responseUser = await fetch(`https://[my Project ID]-default-rtdb.firebaseio.com/users.json/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: nombre,
            correo: email,
            image: foto,
            title: 'Albo Aficionado',
            acceso: 0
        })    
    });
    
    console.log(responseUser);
};

当我在模拟器中点击上传或创建用户时,我收到一个未显示在终端中的错误:

当我检查我的 Firebase 时,我发现 Auth 用户已创建:

图片已正确上传(因为它使用用户 ID 作为名称)。

但在实时数据库中查看时,没有与该 UID 匹配的记录:

我已经使用 PostMan 测试了 End Point,它至少可以使用 Postman。

最后要记住一些关于我的问题的事情:

  1. 我已经尝试在注册功能上同时使用函数 WriteUser 或该函数的内容,但这些方法都不起作用。

  2. React Firebase Expo SDK 并不适合我。

  3. 数据库插入正在通过 Postman 进行,因此 Endpoint 正在工作。

  4. 似乎代码只是绕过了插入集合的那部分代码。

关于我做错了什么以及如何解决它的任何想法?

亲切的问候

【问题讨论】:

    标签: javascript firebase react-native firebase-realtime-database


    【解决方案1】:

    PUTting 的 URL 是错误的:

    fetch(`https://[my Project ID]-default-rtdb.firebaseio.com/users.json/${id}`)
    

    应该是

    fetch(`https://[my Project ID]-default-rtdb.firebaseio.com/users/${id}.json`)
    

    所以.json 在 URL 的末尾,而不是 URL。

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2022-06-16
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      相关资源
      最近更新 更多