我使用mern堆栈创建了一个简单的 crud 应用程序。我可以将项目保存到 dynamodb 但无法通过id. 该应用程序使用一个简单的项目前端save/edit/delete。应用程序.jsapp.use('/api/event', event);事件.jsrouter.get('/:id', (req, res) => { const id = req.params.id; const params = { TableName: EVENTS_TABLE, Key: { id } }; dynamoDb.get(params, (error, result) => { if (error) { res.status(400).json({ error: 'Error retrieving Event' }); } if (result.Item) { res.json(result.Item); } else { res.status(404).json({ error: `Event with id: ${id} not found` }); } });});表模型{ "TableName": "events", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }}错误:id 未定义我的代码: https ://github.com/omarhassanhub/mern-eventAngular 代码:所有事件的链接<tbody> {this.state.events.map(event => <tr> <td> <Link to={`/show/${event._id}`}>{event.date} </Link> </td> <td>{event.time}</td> <td>{event.title}</td> </tr> )}</tbody>尝试event通过 id 获取: componentDidMount() { axios.get('/api/event/'+this.props.match.params.id) .then(res => { this.setState({ event: res.data }); console.log(this.state.event); }); }
2 回答

呼唤远方
TA贡献1856条经验 获得超11个赞
在这一行,您绑定了一个包含错误数据的列表
<Link to={`/show/${event._id}`}>{event.date} </Link> </td> <td>{event.time}</td>
你的event
包含id
字段而不是_id
(你的表模型说)
然后,当您尝试在this.props.match.params.id
( axios.get('/api/event/'+this.props.match.params.id)
) 处获取事件 ID 时,id
将是undefined
.
要解决此问题,只需生成具有正确数据的事件列表:
<Link to={`/show/${event.id}`}>{event.date} </Link> </td> <td>{event.time}</td>

慕桂英546537
TA贡献1848条经验 获得超10个赞
您必须定义请求键的列名。更改您的参数对象,使用列名的键表达式并尝试。
const params = {
TableName: EVENTS_TABLE,
Key: {
'id':id
}
};
添加回答
举报
0/150
提交
取消