AWS DAX Client promise() Error: Request object already used

0

Thank you! for taking time to read this post.

I am new to JS and stuck in a callback issue related to promise() when querying AWS DAX. There is a post (using the .promise() call on a dax-services dynamodb document client returns error: Error: ValidationException: Request object already used) related to this issue but am not able to follow the answer.

I also visited below page but got stuck as the page does not provide a sample for client.send(command) https://www.npmjs.com/package/@aws-sdk/client-dax

I keep getting below error: DaxClientError: ValidationException: Request object already used.

Can you please help correcting below code and with explanation to the solution?

I will truly appreciate your help. I spent an entire day on this but did not make any progress.

const express = require('express');
const AWS = require('aws-sdk');
const AmazonDaxClient = require('amazon-dax-client');

var region = "us-west-2";

AWS.config.update({
  region: region
});

const myendpoint = "daxs://mydax.xxxx.dax-clusters.us-west-2.amazonaws.com";
const dax = new AmazonDaxClient({endpoints: [myendpoint], region: region});

// If using AWS.DynamoDB.DocumentClient ...
const doc = new AWS.DynamoDB.DocumentClient({service: dax});

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.get('/readreview', async (req, res) => {
  
try{

var myresult='empty';
    
var params = {
            TableName: 'test',
            Key:{
                "id": 'p12',
                "key": 'description'
            }
        };

await doc.get(params, function(err, data) {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
            } else {
              myresult=data;
           console.log("@@@@@@@@@"+data);
        }
    }).promise();

    res.send(myresult);
}

catch (e) {
  console.log(e);
}
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
sr73
demandé il y a 2 ans230 vues
1 réponse
1

This problem is caused by using both a callback and the .promise() method, like this:

await doc.get(params, function(err, data) {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
            } else {
              myresult=data;
           console.log("@@@@@@@@@"+data);
        }
    }).promise();

The problem is that every call in the DynamoDB API returns an object of type AWS.Request. The actual HTTP request gets sent only when you call the send() method. But passing a callback implicitly calls send(), and calling promise() implicitly calls it again. You can only call send() once.

The callback has to be turned into a .then() call.

await doc.get(params).promise()
       .then(data=>{
              myresult=data;
        }).catch(err=>{
             console.log(err);
       });
profile pictureAWS
EXPERT
répondu il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions