简体   繁体   中英

Cosmos DB Query Works in Data Explorer But Not Node.js

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: "testuser@gmail.com"
        }
    ]
};

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM