javascript - DynamoDB 未答复的更新请求
问题描述
我正在尝试在 DynamoDB 表中执行更新,但由于某种原因,尽管正确编写了查询(参数),但我得到了一个空对象。
该项目是用 Express 构建的,其 MVVM 设计模式类似于 Sprint 中通常使用的设计模式。
重要的是要提到,每当我通过“控制器 - 服务 - 存储库”以单一状态而不是数组执行更新时,它都会起作用。另一方面,当我通过“控制器 - 服务 - 服务 - 存储库”执行流程时,执行失败。我猜这是同步的问题,但我不确定到底在哪里。
目录路径:
├───Handler.js
│ ├───Controller
│ ├───exampleController.js
│ └───Service
│ ├───exampleService.js
│ ├───singleUpdateService.js
│ └───Repository
│ ├───exampleRepository.js
邮递员用例:
{
"id": "21313",
"text": "text string",
"arrayData": [true]
}
Handler.js 代码示例:
...
app.use('/api/example', exampleController);
...
exampleController.js 代码示例:
...router and express import
router.post('/example', async (req, res) => {
res.send(await exampleService.updateExample(req));
});
..
exampleService.js 代码示例:
这个函数接收一个布尔状态数组,这些状态必须被插入到数据库中,使用另一个服务层的函数。
控制器 - 服务 - 服务 - 存储库
...
exports.updateExample = (req) => {
const { id, text, arrayData } = JSON.parse(req.body.toString('utf8'))
//the singleState value is taken from arrayData using a .map
// mapping function is commented as the values are explicitly passed as arguments
//...arrayData.map( singleState =>{
return singleUpdateService.singleUpdateExample(id, text, singleState); //singleState = arrayData[0]
//...});
};
...
singleUpdateService.js 代码示例:
...
exports.singleUpdateExample= (id, text, state) => {
const params = {
TableName: 'table',
Key: {
'id': { 'S': id },
'text': text
},
UpdateExpression: 'set state = :state',
ConditionExpression: 'text = :text',
ExpressionAttributeValues: {
':state': state,
':text': text
},
ReturnValues: "UPDATED_NEW"
};
return exampleRepository.update(params);
}
...
exampleRepository.js 代码示例:
const aws = require('aws-sdk');
const docClientUpd = new aws.DynamoDB.DocumentClient();
exports.update = (params) => {
return docClientUpd.update(params, async (err, result) => {
if (err)
return err;
return result;
}).promise();
}
谢谢您的支持。
解决方案
推荐阅读
- php - 如何在 Laravel 中将数组数据从控制器传递到刀片
- javascript - 如何在 RichEmbed/MessageEmberd 中发送 gif
- php - Wordpress cookie 只能从一个页面获得,即使它是为整个域设置的
- python - ERROR:process_reader_win.cc(123)] NtOpenThread: {Access Denied} 尝试使用 Selenium 在 Python 上运行图像下载器
- trust - 如何在两个 freeIPA 实例之间设置跨领域信任
- python - Why GUI should use call_soon_threadsafe() when talking to asyncio loop
- javascript - 如何正确调用从第三方脚本导入的 js 模块
- html - 如何在烧瓶 wtf 中使用自定义标签?
- google-cloud-platform - 在 gitops 原理中使用 Gitlab、Terraform、Flux 和 GKE 的最佳方式
- java - 无法在多人游戏中注册自定义剑