首页 > 解决方案 > 如何使用存储过程将项目添加到文档集合

问题描述

我想使用存储过程将项目集合添加到 cosmos DB 文档中。我实现的存储过程似乎有问题。我错过了什么。

我收到错误“无法反序列化存储过程响应或将其转换为类型‘System.Int32’:将值 {null} 转换为类型‘System.Int32’时出错”

我正在使用下面的存储过程来做同样的事情。但徒劳无功。

function spBulkAddStockCountItems(tenantId, stockCountId, stockCountItems) {

    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();

    var count = 0;

    // Validate input.
    if (!stockCountItems) throw new Error("The array is undefined or null.");

    var stockCountItemsLength = stockCountItems.length;
    if (stockCountItemsLength === 0) {
        getContext().getResponse().setBody(0);
        return;
    }

    tryAdd(stockCountItems[count], callback);

    function tryAdd(doc, callback) {

        var queryDocument = "select * from c where c.tenantId='" + tenantId + "' and c.id='" + stockCountId + "'";

        var isAccepted = collection.queryDocuments(collection.getSelfLink(), queryDocument, {},
            function (err, documents, responseOptions) {
                if (err) throw new Error("Error" + err.message);

                if (documents.length != 1) throw "Unable to find both names";
                stockCountDocument = documents[0];

                stockCountDocument.items.push(doc);

                console.log('done');
            });

        if (!isAccepted) getContext().getResponse().setBody(count);
    }

    function callback(err, doc, options) {
        if (err) throw err;

        // One more document has been inserted, increment the count.
        count++;

        if (count >= stockCountItemsLength) {
            // If we have created all documents, we are done. Just set the response.
            getContext().getResponse().setBody(count);
        } else {
            // Create next document.
            tryAdd(stockCountItems[count], callback);
        }
    }
}

这是我要添加项目的文档。

 {
        "items": [],
        "action": null,
        "status": "InComplete",
        "startTime": "2018-05-03T15:34:57.0237016+05:30",
        "finishTime": "0001-01-01T00:00:00",
        "supervisor": {
            "userName": null,
            "phoneNumber": null,
            "pin": null,
            "roleId": "00000000-0000-0000-0000-000000000000",
            "outlets": null,
            "swipeCardInfo": null,
            "id": "00000000-0000-0000-0000-000000000000",
            "name": "admin "
        },
        "outletId": "1884e251-7332-4ff6-9bc6-9b977521fd56",
        "tenantId": "test@testimport6",
        "type": "TIGER.Domain.Models.Stock.StockCount",
        "auditLogs": null,
        "isDeleted": false,
        "id": "9c46c625-2448-4170-b7a2-23ced726f23f",
        "name": null,
        "_rid": "4FsbAPwjFQKcAgAAAAAAAA==",
        "_self": "dbs/4FsbAA==/colls/4FsbAPwjFQI=/docs/4FsbAPwjFQKcAgAAAAAAAA==/",
        "_etag": "\"0000ac54-0000-0000-0000-5aeaded30000\"",
        "_attachments": "attachments/",
        "_ts": 1525341907
    }

标签: javascriptazureazure-cosmosdb

解决方案


我将存储过程更新为并且它可以工作....

function spBulkAddStockCountItems(tenantId, stockCountId, stockCountItems) {

    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    var count = 0;

    var stockCountItemsLength = stockCountItems.length;

    // Validate input.
    if (!stockCountItems) throw new Error("The array is undefined or null.");

    if (stockCountItemsLength === 0) {
        getContext().getResponse().setBody(0);
        return;
    }

    tryAdd(stockCountItems[count], callback);

    function tryAdd(stockCountItem, callback) {

        var queryDocument = "select * from c where c.tenantId='" + tenantId + "' and c.id='" + stockCountId + "'";

        var isAccepted = collection.queryDocuments(collection.getSelfLink(), queryDocument, {},
            function (err, documents, responseOptions) {

                if (err) throw new Error("Error" + err.message);

                documents[0].items.push(stockCountItem);

                var isReplaced = collection.replaceDocument(documents[0]._self, documents[0], callback);

                if (!isReplaced) getContext().getResponse().setBody(count);
            });
        if (!isAccepted) getContext().getResponse().setBody(count);
    } 

    function callback(err, doc, options) {
        if (err) throw err;

        // One more document has been inserted, increment the count.
        count++;

        if (count >= stockCountItemsLength) {
            // If we have created all documents, we are done. Just set the response.
            getContext().getResponse().setBody(count);
        } else {
            // Create next document.
            tryAdd(stockCountItems[count], callback);
        }
    }

}

推荐阅读