admin管理员组

文章数量:1315999

I'm using put and get without problems, but when it es to delete, nothing happens. Here's my code:

async function resetUserIdDB(userId) {
  let params = {
    TableName: 'TableName',
    "Key": {
      "userId": {
        "S": userId.toString()
      }
    }
  };
  try {
    const dbResponse = await ddb.deleteItem(params).promise();
    console.log(`dbresponse has params of ${JSON.stringify(params)} and response of ${JSON.stringify(dbResponse)}`);
    if (dbResponse.Item) {
      console.log(`deleted row with userId of ${userId}`);
      return (dbResponse);
    }
  } catch (err) {
      console.log(`user reset failed with ${err}`);
    throw new Error(`failed to reset because of ${err}`);
  }
}

The params all look fine, but I just get an empty response, and no error, but no deletion either. I'm using the same .promise() on all my other dynamodb actions.

Any ideas?

I'm using put and get without problems, but when it es to delete, nothing happens. Here's my code:

async function resetUserIdDB(userId) {
  let params = {
    TableName: 'TableName',
    "Key": {
      "userId": {
        "S": userId.toString()
      }
    }
  };
  try {
    const dbResponse = await ddb.deleteItem(params).promise();
    console.log(`dbresponse has params of ${JSON.stringify(params)} and response of ${JSON.stringify(dbResponse)}`);
    if (dbResponse.Item) {
      console.log(`deleted row with userId of ${userId}`);
      return (dbResponse);
    }
  } catch (err) {
      console.log(`user reset failed with ${err}`);
    throw new Error(`failed to reset because of ${err}`);
  }
}

The params all look fine, but I just get an empty response, and no error, but no deletion either. I'm using the same .promise() on all my other dynamodb actions.

Any ideas?

Share Improve this question asked Sep 7, 2018 at 17:17 digitaltoastdigitaltoast 6898 silver badges24 bronze badges 3
  • Maybe the item with that Key is not found. – Faizuddin Mohammed Commented Sep 7, 2018 at 17:52
  • Good thinking, but that's why I logged out the params, just to check. The UserId matches fully, for case and type. And if I make a "wrong" param, like UserID, then I get an expected error, like "The provided key element does not match the schema". – digitaltoast Commented Sep 7, 2018 at 19:38
  • can you show the ddb var, dynamodb AWS initialisation ! – Sid Ali Commented Sep 8, 2018 at 15:01
Add a ment  | 

3 Answers 3

Reset to default 4

I just ran into the same problem. It seems like some of the SDK functions don't actually work unless you pass them a callback parameter, even though it's optional. Even a function that does nothing seems to make it work. i.e.

const dbResponse = await ddb.deleteItem(params, () => {}).promise();

When you delete an element in Dynamodb and nothing happen, it means that the key wasn't found, so double check your key.

The table exist, otherwise you would have had an error saying

.amazonaws.services.dynamodb.model.ResourceNotFoundException: Requested resource not found (Service: AmazonDynamoDB; Status Code: 400; Error Code: ResourceNotFoundException; Request ID

For example to delete in the table : TABLEXAMPLE the key : {id: "client", sortingKey: "TTN-BPLAN-7129-6114"}

check that the id exists and is correct, also the sorting key exists and is correct

Not sure, but the following might help to debug in general:

  1. try to remove async/await while testing.
  2. remove 'promise()' after call to 'deleteItem' (try to test one thing at a time).
  3. do not throw from the catch block, but return err.
  4. the "Key" object is in quotes. It may not have to be in quotes just like TableName.

basically, make the code simpler. It will help to debug.

本文标签: javascriptDynamoDB DeleteItem apparently not workingno error givenStack Overflow