【发布时间】:2021-07-27 03:39:21
【问题描述】:
正如标题所解释的,我想使用 id 获取项目。 现在我已经设置了 GET API,它返回 Cosmos DB 中容器内的所有文档。 现在我要做的是设置将使用特定 ID 并返回该 ID 的文档的 GET API。
这是我尝试过的..
app.get('/:id', async (req, res) => {
try {
const dbResponse = await cosmosClient.databases.createIfNotExists({
id: databaseId
});
let database = dbResponse.database;
const { container } = await database.containers.createIfNotExists({id: containerId});
// const querySpec = {
// query: "SELECT * FROM c WHERE c.id = 4040c"
// };
const result = await (database.container.items.id);
console.log(result);
} catch (error) {
console.log(error);
res.status(500).send("Error with database query: " + error.body);
}
})
使用 database.container.items.id 给我错误“TypeError: Cannot read property 'id' of undefined”
我在 postman 中使用以下路由运行 GET 方法:localhost:3000/4040c。 其中 4040c 是存储在数据库中的文档的实际 id。
现在我正在考虑使用 SQL 查询,但我不确定该怎么做,或者即使它是正确的方法。 非常感谢任何提示或指导。谢谢。
【问题讨论】:
标签: api express postman azure-sql-database azure-cosmosdb