使用存储过程

时间:2018-04-20 12:06:06

标签: azure azure-cosmosdb

我有获取多个文档。我已将所有文档投射到各自的数据模型中。更新每个文档的属性后,我需要保存存储过程中的所有文档。 我已阅读此方法 replaceDocument(documentLink,document,optionsopt,callbackopt)中的stored procedure Collection的文档。在Casting之后我找不到所需的documentLink。

我已尝试过此功能,但无法正常使用

function bulkReplace(docs) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var count = 0;

if (!docs) throw new Error("The array is undefined or null.");

var docsLength = docs.length;
if (docsLength == 0) {
    getContext().getResponse().setBody(0);
    return;
}

tryCreate(docs[count], callback);

function tryCreate(doc, callback) {
    var isAccepted = collection.replaceDocument(doc._self, doc, callback);

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

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

    count++;

    if (count >= docsLength) {

        getContext().getResponse().setBody(count);
    } else {

        tryCreate(docs[count], callback);
    }
}
}

异常详情:

  

“无法处理请求”,“errorDetails”:“服务器遇到了   处理您的请求时出现问题,请重试   “technicalReason”:“类型:Microsoft.Azure.Documents.DocumentClientException   来源:Microsoft.Azure.Documents.Client消息:消息:   {\“错误\”:[\“执行Javascript时遇到异常。   异常=错误:文档链接无效:\“undefined \”。\ r \ nStack   跟踪:错误:文档链接无效:\“undefined \”。\ n at   validateDocumentLink   (bulkReplace.js:349:21)\ natreplaceDocument(bulkReplace.js:780:17)\ n   在tryCreate(bulkReplace.js:45:9)

1 个答案:

答案 0 :(得分:0)

我在我身边测试了你的代码,但它确实有用。

众所周知,azure document db中的文档有一些自动生成字段,包括"_self"。您不需要在存储过程中进行另一个查询。您只需要确保导入的docs参数中的文档包含正确的"_self"字段,否则会发生invalid document link异常。

例如:

var doc = { "id": "2", 
            "name" : "ccc",
            "_self": "dbs/duUuAA==/colls/duUuAPfBbAA=/docs/duUuAPfBbAAzAQAAAAAAAA==/",
}

我建议您使用console.log()在代码中打印doc._self以检查其值。

希望它对你有所帮助。

相关问题