2

I need to get record (-s) from DynamoDB via Dynamoose by non-key attribute. For example, I need to get all records from the table someModel where the field my_field is equal to 111. I do it by the next way:

const data = await someModel.query("my_field").eq("111").exec();

And this code stops executing! I mean that following code after that is not called.

If I change code to this:

const data = await someModel.query("my_field").eq("111");

my query is working, but data contains:

{
  "options": {
    "all": {
      "delay": 0,
      "max": 1
    }
  },
  "query": {
    "hashKey": {
      "name": "my_field",
      "value": "111"
    }
  },
  "filters": {},
  "buildState": false,
  "validationError": null,
  "notState": false,
  "success": true
}

I understand that above code is prepared query parameters for query but how can I apply them and execute query to DynamoDB? Of course the query code is placing inside async function - that is why await is written there.

I use also serverless framework for describing DynamoDB schemes. But all models I write via dynamoose.

Where am I mistaking?

0

1 Answer 1

1

As mentioned in the documentation here, Dynamoose query returns the value in the callback and not as a promise. Therefore, your response is actually returned in a callback that should come inside the exec part of your query. async/await is valid for promises and not callbacks.

someModel.query("my_field").eq("111").exec((err, data) => {
    // Do whatever you wish to with your data
});
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, @nerdier.js! I saw that docs and I tried to do by this way but got next error: "ValidationException: Query condition missed key schema element: id"
@vitaliy It could be that you aren't including the primary key while running a query over the Dynamodb table. Are you sure my_field is the primary key of the table ?
Of course my_field is non primary key field :) But!!! this field has described as Global Secondary Index, so I expected that query method will work. At this moment I solved my issue with scan method but I want to understand how can I use query method.
To use the query method with Dynamodb, remember that query can only be used with primary keys - at the moment, it is a limitation of th dynamodb. You may also wish to explore the options of creating global and local secondary indexes if in case you don't wish to turn my_field into a primary key. The methods of retrieval would be slightly different.
Hmm... Interesting. Could you provide some example or links where I can read about including another field to primary (maybe is it RANGE key schema?) and about methods for those purposes? Would be very grateful!
|

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.