reactjs - Firebase 实时数据库显示意外行为
问题描述
我正在为我的订单向 firebase 发出 POST 请求,但订单中的“成分”属性没有反映在 firebase 实时数据库中。除此之外,订单中的所有属性都会正确反映。请帮忙。
我的代码截图。
orderHandler = (event) => {
event.preventDefault();
console.log('This is without JSON.stringify', this.props.ingredients);
console.log(
'This is with JSON.stringify',
JSON.stringify(this.props.ingredients)
);
this.setState({ loading: true });
const order = {
price: this.props.price.toFixed(2),
ingredients: this.props.ingredients,
customer: {
name: 'Tarun Singh',
address: {
street: 'TestStreet',
zipCode: 42321,
country: 'India',
},
email: 'test@test.com',
},
deleveryMethod: 'fastest',
};
console.log(
'This is my order before making post request, ingredient property is present here',
order
);
axios
.post('/orders.json', order)
.then((response) => {
this.setState({ loading: false });
this.props.history.push('/');
})
.catch((error) => {
this.setState({ loading: false });
});
};
解决方案
它看起来像这样的代码:
console.log(
'This is with JSON.stringify',
JSON.stringify(this.props.ingredients)
);
输出这个:
这是 JSON.stringify, []
所以看来,当这段代码运行时,你的this.props.ingredients
还没有被填充。
当路径为空/没有值时,Firebase 不会存储它。这就解释了为什么ingredients
您的应用程序的属性没有显示在数据库的 JSON 中。
推荐阅读
- selenium - Selenium C# Extent 报告未在指定文件夹中生成 HTML 文件
- acumatica - 从销售订单摘要报告中排除订单
- powershell - Powershell 数组问题
- spring - Spring 框架:mvc:resources 不起作用
- c# - C# - 确定方法 ReturnType 是否属于特定类型
- javascript - 在 smarty 中使用 {literal} 的最佳实践是什么
- git - 使用 maven-release-plugin 时的管道循环
- exception-handling - 如何生成 SPARC 硬件陷阱(陷阱类型 tt < 128)
- c# - Nethereum C# 如何获得地址余额?
- sql - SQL Server 中关键字“WITH”附近的语法不正确