0
votes

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.');
}

it always get error code 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 ?

3

3 Answers

1
votes

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.');
}
0
votes

Can you try this: var docId = collection.getAltLink() + 'docs/myDocId'; -- self link is not for "name routing".

0
votes

You could modify your code like :

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.');
}

Or you could use follow sample code to query the document, it also contains all of the fields.

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.');
}