为了账号安全,请及时绑定邮箱和手机立即绑定

根据 id 从 DynamoDB 中检索项目

根据 id 从 DynamoDB 中检索项目

互换的青春 2022-01-13 16:08:40
我使用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>


查看完整回答
反对 回复 2022-01-13
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

您必须定义请求键的列名。更改您的参数对象,使用列名的键表达式并尝试。


const params = {

    TableName: EVENTS_TABLE,

    Key: {

      'id':id

    }

  };


查看完整回答
反对 回复 2022-01-13
  • 2 回答
  • 0 关注
  • 332 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号