首页 > 解决方案 > 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();
} 

谢谢您的支持。

标签: javascriptapiexpressaws-lambdaamazon-dynamodb

解决方案


推荐阅读