【发布时间】:2020-11-15 20:55:24
【问题描述】:
我正在使用 firebase,我想从我的 react 应用发送数据。我安装了 Axios,然后制作了一个 Axios 组件并使用 Axios 发送数据。但是我收到了 401 未经授权的错误
purchaseContinueHandler = () => {
const order = {
ingredients : this.state.ingredients,
price : this.state.totalPrice,
customer : {
name : 'Usman',
address :{
street : 'wapda town',
zipcode : '54700',
country : 'pakistan'
},
email : 'usidd@gmail.com'
},
deliveryMethod : '30minutes'
}
axios.post('/order.json',order).
then(response =>
console.log(response)).
catch(error =>
console.log(error)); // .json to be added for firebase
}
firebase 的安全规则如下:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 8, 25);
}
}
}
【问题讨论】:
-
来自 API 的未经授权的错误。所以可能是您的 API 需要登录令牌或类似的东西。如果没有登录令牌,您将从 API 收到此类错误。
-
您的实时数据库是否有任何安全规则?
-
@RenaudTarnec 是的,它允许所有人编辑
-
能否将您的安全规则代码添加到您的问题中?
-
谢谢。但是,这些是 Firestore 的安全规则,而不是实时数据库的安全规则(它们是两种不同的数据库服务)。您应该在 Firebase“数据库”控制台面板中,通过靠近“数据库”标题的下拉选择框切换数据库。
标签: javascript reactjs firebase firebase-realtime-database axios