使用React和Axios更新Firebase中的数据 [英] Updating Data in Firebase using React and Axios

查看:116
本文介绍了使用React和Axios更新Firebase中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新Firebase数据库中的某些数据.但是我无法使updateAPI函数正常工作.我相信这与axios.put有关,URL可能无法正确遍历数据?

I'm trying to update some data in a firebase database. However i am unable to get the updateAPI function to work properly. I believe it's something to do with axios.put, the url is probably not traversing the data correctly?

我尝试更改网址,但最终出现OPTIONS和CORS错误,这意味着网址不正确.

I have tried changing the url but end up with a OPTIONS and CORS errors which means the url is incorrect.

        // API Function calls
        async getAPI(){
                const response = await axios.get('https://fir-test-euifhefibewif.firebaseio.com/todos.json')
                console.log(response)
                console.log(response.data)
            }
            async postAPI(){
                const response = await axios.post('https://fir-test-euifhefibewif.firebaseio.com/todos.json', {name: notesAll})
                console.log(response)
                console.log(response.data)
            }
            async deleteAPI(){
                const response = await axios.delete('https://fir-test-euifhefibewif.firebaseio.com/todos.json')
                console.log(response)
                console.log(response.data)
            }
            async updateAPI(){
                const response = await axios.put('https://fir-test-euifhefibewif.firebaseio.com/todos/-LVYEw6KTltVoEkPeuxY/name/0', {note})
                console.log(response)
                console.log(response.data)
            }

        // Notes notesAll
        const notesAll = [
        {
                "name": "user1",
                "race": "human"
            },
            {
                "name": "user2",
                "race": "human"
            },
            {
                "name": "user3",
                "race": "human"
            },
            {
                "name": "user4",
                "race": "human"
            },
            {
                "name": "user5",
                "race": "human"
            }
        ]

        // Test data
            const note = [
            {
                name: 'Test',
                race: 'Test'
            }
        ]

        // Firebase database example
        {
        "todos": {
            "-LVYIfktKR6fa6ish1aw": {
            "name": [
                {
                "name": "user1",
                "race": "human"
                },
                {
                "name": "user2",
                "race": "human"
                },
                {
                "name": "user3",
                "race": "human"
                },
                {
                "name": "user4",
                "race": "human"
                },
                {
                "name": "user5",
                "race": "human"
                }
            ]
            }
        }
        }

所有代码所需要做的就是更新对象数组中的一些数据.

All the code needs to do is update some data inside of an array of objects.

推荐答案

尝试:

    async updateAPI(){
        const response = await axios.put('https://fir-test-euifhefibewif.firebaseio.com/todos/-LVYEw6KTltVoEkPeuxY/name/0', note)
        console.log(response)
        console.log(response.data)
    }

    // Test data
    const note = {
        name: 'Test',
        race: 'Test'
    }

要使用REST写入Firebase数据库,URL必须以.json结尾.所以:

To write to Firebase Database using REST, the URL must end in .json. So:

url = yourUrl + ".json";

在此处查找更多信息: https://firebase.google.com/docs/database/rest/save-data

Find more information here: https://firebase.google.com/docs/database/rest/save-data

这篇关于使用React和Axios更新Firebase中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆