0

I am trying to run the following query against my cosmos db using Node.js.

const querySpec = {
    query: "SELECT * FROM Users u WHERE u.id = @email",
    parameters: [
        {
            name: "@email",
            value: "[email protected]"
        }
    ]
};

const { result: results } = client.database(databaseId).container(containerId).items.query(querySpec).toArray();
if (results.length == 0) {
    throw "No matching user";
} else if (results.length > 1) {
    throw "Account found";
}

const user = results[0];
console.log(user);

however I keep getting the error TypeError: results is undefined. The query works just fine in the data explorer. databaseId and containerId print the values I need them to if I use console.log.

Why might i be getting this error?

0

1 Answer 1

2

I believe the reason you're getting this error is because query is an async method and you're not awaiting it. Can you try by changing the following line of code:

const { result: results } = client.database(databaseId).container(containerId).items.query(querySpec).toArray();

to:

const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec).toArray();

and see if that takes care of the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to put query inside an async function as well then call it, but this worked. Thank you!

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.