Cosmos DB查询可在数据资源管理器中运行,但不能在Node.js中运行 [英] Cosmos DB Query Works in Data Explorer But Not Node.js
问题描述
我正在尝试使用Node.js对我的cosmos数据库运行以下查询.
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);
但是我一直收到错误 TypeError:结果未定义
.该查询在数据资源管理器中工作正常.如果我使用console.log,则 databaseId
和 containerId
会打印需要它们的值.
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?
推荐答案
我相信您收到此错误的原因是因为 query
是 async
方法,并且您不在等待.您可以通过更改以下代码行来尝试:
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();
收件人:
const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec).toArray();
看看是否可以解决问题.
and see if that takes care of the issue.
这篇关于Cosmos DB查询可在数据资源管理器中运行,但不能在Node.js中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!