-1

This may sound ridiculous to ask. But on a serious note, I am putting in an item in DynamoDB successfully using PutItemCommand. I confirm it by scanning for items in AWS console.

But on querying it, the QueryCommand is not returning any item (empty array)

Primary key is comprised of _type partition key and uid sort key. The sort key is hash (string)

Here is my code for querying to get al items with specific partition key:

import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({...credentials});
const res = await client.send(new QueryCommand({
   TableName: SOME_NAME,
   ScanIndexForward: false,
   KeyConditionExpression: "#_type = :type AND uid > :uid",
   ExpressionAttributeValues: marshall({
      ":type": "apple",
      ":uid": "0"
   }),
   ExpressionAttributeNames: {
      "#_type": "_type"
   },
}));

if(res) {
   return res.items.map(item => unmarshall (item));
}else{
   throw Error("Failed to fetch");
}

The item in dynamodb to query against has primary key: _type = 'apple' and uid = 'dh46dhdj3jdhd738'

What is possibly wrong?

6
  • This looks fine to me, but something doesn't add up. Either you're not sharing the entire story or something else is at play. Are you putting the item in just before querying? Can you see the item from your app if you use Scan? Commented Jan 24 at 20:31
  • Yes, I put the item a few seconds before querying it. But then I wait and query again, and wait and again. And it doesn't show up. No, this is the code. I've shared the item I try to fetch from query Commented Jan 25 at 3:38
  • Hey, downvoting won't do. It's legit case. All seems fine but the items aren't returning Commented Jan 25 at 8:23
  • I didn't downvote, why would I. Do one thing, make your request strongly consistent and let me know the results. Commented Jan 25 at 8:55
  • I did that. Nothing changes. I have a feeling that it is then the underscore prefix fiddling with the dynamodb caching logic. Because, directly setting it in KeyConditionExpression as a partition key caused an error. Someone raised this issue here, and the solution was to use ExpressionAttributeNames. Is it really a bug? As far as the code goes, it has been double checked. The items get queried successfully from the console with same conditions. They are in the table for hours now. Commented Jan 25 at 10:36

1 Answer 1

0

This looks to be happening because you're using the low level client but build your request using the high level client syntax.

Have a read of this blog:

https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/

import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({...credentials});
const res = await client.send(new QueryCommand({
   TableName: SOME_NAME,
   ScanIndexForward: false,
   KeyConditionExpression: "#t = :type AND uid > :uid",
   ExpressionAttributeValues: marshall({
      ":type": "apple",
      ":uid": "0"
   }),
   ExpressionAttributeNames: {
      "#t": "_type"
   },
}));

if(res) {
   return res.items.map(item => unmarshall (item));
}else{
   throw Error("Failed to fetch");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for answering. But it is not correct. Either way it should work. If I am using low -level client then I am also making sure about dynamodb query syntax. I did try with high-level client, but it's too returning an empty array. I'm using ap-south-1 region. Is there any temp technical issue on your end in that heavily used region?
Share the exact code you tried in your question. Also share the item you're trying to read.
I have updated the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.