我正在尝试使用一种非常简单的方法从我的 reactjs 应用程序向 firebase 实时数据库发布请求。这是一个进入我的数据库的屏幕截图。这里 blogs 下的第一个条目是用户的 UID,该用户可以创建多个博客,例如屏幕截图中的 id b1。
这是我的 POST 请求代码:
const id = localStorage.getItem('uid')
const postData = async () => {
var num = localStorage.getItem('blogNumber')
const resp = await fetch(
'https://assignment-c3557-default-rtdb.asia-southeast1.firebasedatabase.app.firebaseio.com/blogs.json',
{
method: 'POST',
body: {
id: {
num : {
title: titleRef.current.value,
description: contentRef.current.value,
createdAt: new Date(),
isPosted: false
}
}
},
headers: {
'Content-Type': 'application/json'
}
});
const data = await resp.json()
localStorage.setItem('blogNumber', localStorage.getItem('blogNumber') +1)
}
尝试点击此网址时收到 400 个错误请求
但根据这个页面,这似乎是我正在点击的正确网址
https://retool.com/blog/your-guide-to-crud-in-firebase-realtimedb-with-rest-api/