【问题标题】:Cosmos DB Query Works in Data Explorer But Not Node.jsCosmos DB 查询可在数据资源管理器中使用,但不适用于 Node.js
【发布时间】:2019-05-16 14:37:48
【问题描述】:

我正在尝试使用 Node.js 对我的 cosmos db 运行以下查询。

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: results is undefined。该查询在数据资源管理器中运行良好。如果我使用 console.log,databaseIdcontainerId 打印我需要的值。

为什么会出现这个错误?

【问题讨论】:

    标签: javascript node.js azure azure-cosmosdb azure-cosmosdb-sqlapi


    【解决方案1】:

    我相信您收到此错误的原因是因为 queryasync 方法,而您没有等待它。您可以通过更改以下代码行来尝试吗:

    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();
    

    看看能不能解决这个问题。

    【讨论】:

    • 我不得不将query 放在async 函数中,然后调用它,但这有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多