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

为君一笑 提交于 2019-12-08 12:45:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/56162063/cosmos-db-query-works-in-data-explorer-but-not-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!