javascript - 使用 React 和 Axios 更新 Firebase 中的数据

标签 javascript reactjs firebase firebase-realtime-database axios

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

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

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

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

最佳答案

尝试:

    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 结尾。所以:

url = yourUrl + ".json";

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

关于javascript - 使用 React 和 Axios 更新 Firebase 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062203/

相关文章:

javascript - Nodejs 和 Expressjs : Constants in global variable

javascript - 将 watchify 与 babelify 一起使用

javascript - 使用reactjs进行多排序表

node.js - 如何向 Firebase 验证服务器?

javascript - 如何将弹出框内的输入标签绑定(bind)到 Vue 模型

javascript - 当我使用异步/等待功能时,Angular UI 路由器不处理解析功能?

javascript - html5 canvas手形光标问题

javascript - SVG 箭头动画

javascript - 公理 : Request header field Access-Control-Allow-Methods is not allowed by Access-Control-Allow-Headers in preflight respones

swift - 将数据写入 Firebase 不起作用 - 无法将类型 'user' 的值转换为预期参数类型 'AnyObject?'