node.js 用于管理 Azure Cosmos db 教程代码的控制台应用程序代码抛出错误为未定义
node.js console app code to manage Azure Cosmos db tutorial code throwing error as undefined
我是 运行 来自 Azure 文档中官方教程的 node.js 控制台应用程序。请在下面找到 link:
https://docs.microsoft.com/en-us/azure/cosmos-db/sql/sql-api-nodejs-get-started
代码如下:
const cosmos = require("@azure/cosmos");
const CosmosClient = cosmos.CosmosClient;
const endpoint = "******"; // Add your endpoint
const masterKey = "******"; // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, auth: { masterKey } });
const databaseId = "brondbid"; // Add the Database ID
const containerId = "broncollectid"; // Add the Container ID
const querySpec = {
query: "SELECT * FROM c"
};
async function run() {
const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec, { enableCrossPartitionQuery: true }).toArray();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
console.log(`\tQuery returned ${resultString}\n`);
}
}
async function handleError(error) {
console.log("\nAn error with code '" + error.code + "' has occurred:");
console.log("\t" + error.body || error);
}
run().catch(handleError);
当我在终端 运行 节点 app.js 时,出现此错误。
An error with code 'undefined' has occurred:
undefined
请试试这个代码:
const cosmos = require("@azure/cosmos");
const CosmosClient = cosmos.CosmosClient;
const endpoint = "******"; // Add your endpoint
const masterKey = "******"; // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, key: masterKey });
const databaseId = "brondbid"; // Add the Database ID
const containerId = "broncollectid"; // Add the Container ID
const querySpec = {
query: "SELECT * FROM c"
};
async function run() {
const database = client.database(databaseId);
const container = database.container(containerId);
const { resources: results } = await container.items.query(querySpec, { enableCrossPartitionQuery: true }).fetchAll();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
console.log(`\tQuery returned ${resultString}\n`);
}
}
function handleError(error) {
console.log(error);
console.log("\nAn error with code '" + error.code + "' has occurred:");
console.log("\t" + error.body || error);
}
run().catch(handleError);
基本上您的代码失败了,因为查询结果中没有可用的 .toArray()
方法。由于它不是 REST API 错误,因此您将获得 undefined
错误代码。
此外,查询 returns resources
键中的数据,而不是像您正在使用的 result
键中的数据。
我是 运行 来自 Azure 文档中官方教程的 node.js 控制台应用程序。请在下面找到 link: https://docs.microsoft.com/en-us/azure/cosmos-db/sql/sql-api-nodejs-get-started 代码如下:
const cosmos = require("@azure/cosmos");
const CosmosClient = cosmos.CosmosClient;
const endpoint = "******"; // Add your endpoint
const masterKey = "******"; // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, auth: { masterKey } });
const databaseId = "brondbid"; // Add the Database ID
const containerId = "broncollectid"; // Add the Container ID
const querySpec = {
query: "SELECT * FROM c"
};
async function run() {
const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec, { enableCrossPartitionQuery: true }).toArray();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
console.log(`\tQuery returned ${resultString}\n`);
}
}
async function handleError(error) {
console.log("\nAn error with code '" + error.code + "' has occurred:");
console.log("\t" + error.body || error);
}
run().catch(handleError);
当我在终端 运行 节点 app.js 时,出现此错误。
An error with code 'undefined' has occurred:
undefined
请试试这个代码:
const cosmos = require("@azure/cosmos");
const CosmosClient = cosmos.CosmosClient;
const endpoint = "******"; // Add your endpoint
const masterKey = "******"; // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, key: masterKey });
const databaseId = "brondbid"; // Add the Database ID
const containerId = "broncollectid"; // Add the Container ID
const querySpec = {
query: "SELECT * FROM c"
};
async function run() {
const database = client.database(databaseId);
const container = database.container(containerId);
const { resources: results } = await container.items.query(querySpec, { enableCrossPartitionQuery: true }).fetchAll();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
console.log(`\tQuery returned ${resultString}\n`);
}
}
function handleError(error) {
console.log(error);
console.log("\nAn error with code '" + error.code + "' has occurred:");
console.log("\t" + error.body || error);
}
run().catch(handleError);
基本上您的代码失败了,因为查询结果中没有可用的 .toArray()
方法。由于它不是 REST API 错误,因此您将获得 undefined
错误代码。
此外,查询 returns resources
键中的数据,而不是像您正在使用的 result
键中的数据。