使用React和Axios在Firebase中更新数据

时间:2019-01-06 13:56:14

标签: javascript reactjs firebase firebase-realtime-database axios

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

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

        // 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"
                }
            ]
            }
        }
        }

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

1 个答案:

答案 0 :(得分:1)

尝试:

ToString

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

DisplayMember

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