DynamoDB查询返回description显示[Array]及前端报错问题求助
问题解决指南
1. 控制台输出中description显示为[Array]的问题
这是Node.js控制台的默认格式化优化行为,当对象内的数组元素较多或存在嵌套时,会用[Array]简化显示,实际数据本身是完整的数组。要查看真实内容,可通过两种方式:
- 使用
JSON.stringify格式化输出完整数据:console.log(JSON.stringify(data, null, 2)); - 在Node.js控制台中点击输出的对象,手动展开即可查看数组的具体内容。
2. 前端报错Uncaught TypeError: Cannot read properties of undefined (reading 'tagName')的问题
根源分析
getAllItems函数声明为async,但内部使用了docClient.query的回调模式,既没有返回Promise,也没有将查询结果返回。导致await getAllItems()得到的是undefined,前端收到undefined后尝试访问tagName时触发错误。
修复方案
将docClient.query改为Promise风格调用,确保函数返回查询到的数据:
修改后的getAllItems代码
var AWS = require("aws-sdk"); AWS.config.update({ region: "local", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient() var table = "Tourism"; const getAllItems = async () => { var PKContinent = "PKContinent"; const params = { TableName: table, KeyConditionExpression: "PKContinent = :pkUpdate AND begins_with(SKCountry, :SKCountry)", ExpressionAttributeValues: { ":pkUpdate": PKContinent, ":SKCountry": "Country#" } } try { // 改用Promise方式获取查询结果 const data = await docClient.query(params).promise(); console.log(JSON.stringify(data, null, 2)); // 查看完整的description数组内容 if (data.Items.length === 0) { console.log("data doesn't exists."); return []; } return data.Items; // 返回查询到的Items列表 } catch (error) { console.log(error); throw error; // 抛出错误供路由层捕获处理 } } module.exports = { docClient, getAllItems };
路由代码说明
原路由代码无需修改,现在await getAllItems()会正确返回包含完整description数组的Items列表,前端能拿到有效数据,不会再因undefined触发报错。
内容的提问来源于stack exchange,提问作者poonamtomar34
相关产品推荐
相关产品推荐

