javascript - 如何在 React JSX 中映射包含 JSON 对象的数组?
问题描述
我在我的 redux 操作中从 firebase 存储获取 JSON 对象。我将 JSON 对象存储在关联数组中。当我尝试执行 this.props.expenses 时,我会取回数组。
但我无法在 JSX 中映射数组??
import { SHOW_EXPENSE } from './types';
import database from '../config/firebase';
export const showExpenses = () => dispatch => {
const expenses = [];
database.ref()
.once('value')
.then((snapshot) => {
snapshot.forEach((childSnapshot) => {
expenses.push({
id: childSnapshot.key,
...childSnapshot.val()
})
});
});
console.log(expenses);
dispatch({
type: SHOW_EXPENSE,
expenses : expenses
})
return expenses;
};
这是我的 console.log(expenses);
0: {id: "-LUumVQzeXBN8iK81EVO", expense_amount: "500", expense_name:
"bike service"}
1: {id: "-LUumeeCeYIdd2G4Pz5K", expense_amount: "1500",
expense_name: "tire change"}
length: 2__proto__:
Array(0)
当我尝试映射 this.props.expenses 时,它什么也不返回。
{this.props.expenses.map( data =>
{data.expense_amount}
{data.expense_name}
});
解决方案
推荐阅读
- css - 将角材料表中的一个 mat-header-cell 与排序标题对齐
- postgresql - 处理 jsonb_populate_record 的输出
- javascript - 非活动选项卡似乎正在删除侦听器
- javascript - 如何将逗号分隔符放入十进制数
- java - REST 模板无法解析带有冒号 (':') 的字符串响应
- java - 当我执行插入查询 RDF4J 时如何获取 UpdateExpr
- jquery - 获取 JSON 响应并根据 JSON 中的特定键插入表格单元格
- nsis - 其他用户(非管理员用户)的开始菜单/系统托盘中未显示已安装的 NSIS 应用程序
- angular - 为什么我得到一个函数而不是 Ionic 4 上 currentUser 的 UID 值?
- php - 尝试从 PHP 写入 MySQL 数据库时出错,而从数据库读取工作正常