2017-09-05 146 views
1

我正嘗試在使用node.js的Azure Cosmos數據庫中創建存儲過程。創建存儲過程是成功的,但試圖執行它時(從javascript/nodejs),它不返回任何文檔。Azure Cosmos DB存儲過程不返回文檔

首先,我定義的存儲過程,然後在宇宙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)); 
    } 
}); 

我越來越沒有錯誤在控制檯。只是一個空陣列。另外,還有文件中的收集;-)

有沒有人知道我在這裏失蹤?

在此先感謝。

編輯:

從存儲過程獲取這些響應頭:

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門戶網站查詢編輯器查詢時,我得到的結果如預期:

enter image description here

我的分區鍵如下:

enter image description here

+0

不應該'serverScript'屬性名稱是在你的代碼'body'? –

+0

@GauravMantri嗯,不是根據這篇文章,它似乎? :-) https://docs.microsoft.com/en-us/azure/cosmos-db/programming –

+0

我的基礎是:https://docs.microsoft.com/en-us/rest/api/documentdb /創建-A-存儲過程。實際上,我們使用的是Node SDK,我們使用'body'屬性來指定函數體。 –

回答

0

您是否嘗試將分區鍵設置爲實際值?這對我有效。我給我的分區密鑰名稱存儲過程的輸入,但我實際上給了它一個分區鍵值(因爲存儲過程在特定的分區上運行),它的工作。

編輯:

只是看着意見和拉夫似乎表明這種方法也