Cosmos db readDocument API在存储过程中不起作用 [英] Cosmos db readDocument api not work in stored procedure
问题描述
使用CosmosDB/DocumentDB中的 readDocument
函数的简单存储过程,但是不起作用.
A simple stored procedure using readDocument
function in CosmosDB/DocumentDB, but it does not work.
function testRead() {
var collection = getContext().getCollection();
var docId = collection.getSelfLink() + 'docs/myDocId';
// Query documents and take 1st item.
var isAccepted = collection.readDocument(docId, {}, function (err, doc, options) {
if (err) throw err;
response.setBody(JSON.stringify(doc));
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
它总是显示错误代码400.
it always get error code 400.
{"code":400,"body":"{\" code \:\" BadRequest \,\" message \:\"消息:{\\错误\\":[\\执行Java脚本时遇到异常.异常=错误:创建请求消息时出错\\ r \\ nStack跟踪:错误:在readDocument上创建请求消息时出错\\ n(testRead.js:512:17)\\ n在testRead(testRead.js:8:5)\\ n在__docDbMain(testRead.js:18:5)\\ n位于全局代码(testRead.js:1:2)\\]} \ r \ nActivityId:2fb0f7ef-c192-4b56-b8bb-9681c9f8fa6e,请求URI:/apps/DocDbApp/services/DocDbServer22/partitions/a4cb4962-38c8-11e6-8106-8cdcd42c33be/replicas/1p/,RequestStats:,SDK:Microsoft.Azure.Documents.Common/1.22.0.0 \}","activityId":"2fb0f7ef-c192-4b56-b8bb-9681c9f8fa6e","substatus":400}
{"code":400,"body":"{\"code\":\"BadRequest\",\"message\":\"Message: {\\"Errors\\":[\\"Encountered exception while executing Javascript. Exception = Error: Error creating request message\\r\\nStack trace: Error: Error creating request message\\n at readDocument (testRead.js:512:17)\\n at testRead (testRead.js:8:5)\\n at __docDbMain (testRead.js:18:5)\\n at Global code (testRead.js:1:2)\\"]}\r\nActivityId: 2fb0f7ef-c192-4b56-b8bb-9681c9f8fa6e, Request URI: /apps/DocDbApp/services/DocDbServer22/partitions/a4cb4962-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.22.0.0\"}","activityId":"2fb0f7ef-c192-4b56-b8bb-9681c9f8fa6e","substatus":400}
任何人都可以帮助我吗?
Anyone can help me ?
推荐答案
根据迈克尔的建议,我的示例现在可以工作了,下面是代码
According to Michael's suggestion, my sample works now, here is the code
function testRead() {
var collection = getContext().getCollection();
var response = getContext().getResponse();
var docId = collection.getAltLink() + '/docs/myDocId';
// Query documents and take 1st item.
var isAccepted = collection.readDocument(docId, {}, function (err, doc, options) {
if (err) throw err;
response.setBody(JSON.stringify(doc));
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
这篇关于Cosmos db readDocument API在存储过程中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!