Azure Cosmos DB存储过程不返回文档

时间:2017-09-05 09:18:49

标签: azure stored-procedures azure-cosmosdb

我尝试使用node.js在Azure Cosmos DB中创建存储过程。创建存储过程是成功的,但在尝试执行它时(从javascript / nodejs),它不返回任何文档。

首先,我定义存储过程,然后在Cosmos DB中注册它:

var DocumentDBClient = require('documentdb').DocumentClient;

var host = "our-hostname";  // Add your endpoint
var masterKey = "our-master-key"; // Add the masterkey of the endpoint

var client = new DocumentDBClient(host, {masterKey: masterKey});

var powerPlantDataReadingStoredProc = {
    id: "GetPowerPlantDataReadingsSproc",
    serverScript: function() {           
        var context = getContext();           
        var collection = context.getCollection();   
        var request = context.getRequest();
        var response = context.getResponse();

        var isAccepted = collection.queryDocuments(collection.getSelfLink(),
                    'SELECT TOP 1 * FROM c',
                    function(err, feed, options) {
                        response.setBody(JSON.stringify(feed));
                    });

        if(!isAccepted) throw new Error("The query was not acceted by the server");
     }
};

// Register stored proc
var createdStoredProc;

client.createStoredProcedure('dbs/PowerPlantDataReadings/colls/DataReadings', powerPlantDataReadingStoredProc, function(err, sproc) {
    createdStoredProc = sproc;
});  

我用来执行它的代码:

client.executeStoredProcedure("dbs/PowerPlantDataReadings/colls/DataReadings/sprocs/GetPowerPlantDataReadingsSproc", null, { partitionKey: "datareadings" }, function(err, results, responseHeaders) {
    if(err) {
        console.log("Error: ");
        console.log(JSON.stringify(err));
    }

    if(responseHeaders) {
        console.log("Headers: ");
        console.log(JSON.stringify(responseHeaders));
    }

    console.log("Results: " + results);

    if(results) {
        console.log("Results: ");
        console.log(JSON.stringify(results));
    }
});

我在控制台中没有出错。只是一个空数组。此外,集合中还有 个文档; - )

有谁知道我在这里失踪了什么?

提前致谢。

编辑:

从sproc获取这些响应标头:

Headers: {"cache-control":"no-store, no-cache","pragma":"no-cache","transfer-encoding":"chunked","content-type":"application/json","server":"Microsoft-HTTPAPI/2.0","strict-transport-security":"max-age=31536000","x-ms-last-state-change-utc":"Tue, 05 Sep 2017 08:01:38.741 GMT","x-ms-schemaversion":"1.3","x-ms-alt-content-path":"dbs/PowerPlantDataReadings/colls/DataReadings/sprocs/GetPowerPlantDataReadingsSproc","x-ms-content-path":"cjFwAKg0WQA=","x-ms-quorum-acked-lsn":"25347","x-ms-current-write-quorum":"3","x-ms-current-replica-set-size":"4","x-ms-xp-role":"1","x-ms-request-charge":"6.12","x-ms-serviceversion":"version=1.14.89.5","x-ms-activity-id":"604b8d09-ad08-427a-8dee-1b7862e0e092","x-ms-session-token":"0:25347","x-ms-gatewayversion":"version=1.14.89.5","date":"Tue, 05 Sep 2017 09:38:32 GMT","x-ms-throttle-retry-count":0,"x-ms-throttle-retry-wait-time-ms":0} Results: []

此外,在Azure Portal上的查询编辑器中进行查询时,我确实得到了预期的结果:

enter image description here

我的分区键如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否尝试将分区键设置为实际值?这对我有用。我将分区键名称提供给存储过程输入,但实际上我给了它一个分区键VALUE(因为存储过程在特定分区上运行)并且它有效。

编辑:

只看了评论,Gaurav似乎也提出了这种方法