在CosmosDB / DocumentDB中使用readDocument
函数的简单存储过程,但它不起作用。
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。
{“code”:400,“body”:“{\”code \“:\”BadRequest \“,\”message \“:\”消息:{\\“错误\\”:[\\“遇到异常执行Javascript时出现异常=错误:创建请求消息时出错\\ r \\ nStack跟踪:错误:在readReocument(testRead.js:512:17)处创建请求消息\\ n时出错\ test在testRead(testRead.js: 8:5)\\ n at __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”, “子状态”:400}
有人可以帮帮我吗?
根据迈克尔的建议,我的样本现在可以使用,这是代码
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.');
}
你可以试试这个:var docId = collection.getAltLink()+'docs / myDocId'; - 自我链接不适用于“名称路由”。
红衣男子。
你可以修改你的代码,如:
function testRead() {
var collection = getContext().getCollection();
var docId = collection.getAltLink() + 'docs/myDocId';
console.log(collection.getSelfLink() + 'docs/myDocId');
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.');
}
或者您可以使用以下示例代码查询文档,它还包含所有字段。
function testRead() {
var collection = getContext().getCollection();
var query = "select * from c where c.id = '1'";
var isAccepted = collection.queryDocuments(collection.getSelfLink(), query,function (err, doc, options) {
if (err) throw err;
var response = getContext().getResponse();
response.setBody(JSON.stringify(doc));
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
希望它能帮到你。