Azure cosmosDB 默认存储过程不起作用

时间:2021-01-06 07:48:10

标签: javascript azure stored-procedures azure-cosmosdb azure-cosmosdb-sqlapi

当我创建存储过程时,这是 Azure 提供的默认示例代码。我的容器(学生)非常简单,只有 2 个项目。分区键是 id。

{name: "aaa", id: "1"}

{name: "bbb", id: "2"}

这是 Azure CosmosDB 提供的示例存储过程代码。

// SAMPLE STORED PROCEDURE
function sample(prefix) {
    var collection = getContext().getCollection();

    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT * FROM root r',
    function (err, feed, options) {
        if (err) throw err;

        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var body = { prefix: prefix, feed: feed[0] };
            response.setBody(JSON.stringify(body));
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

为什么它总是返回“找不到文档”?我尝试过不同的 sql 查询,例如“select * from Students”或“select * from root”或“select * from c”,但它们都不起作用。

1 个答案:

答案 0 :(得分:1)

当你想执行存储过程时,你需要传递分区键的值。在您的情况下,您需要像这样传递“1”或“2”作为分区键(而不是“id”)的值:

enter image description here