在 DocDb 存储过程中,作为检索要修改的数据的过程的第一步,我读取并使用数据(当且仅当它与 etag 匹配时),如下所示:
collection.readDocument(reqSelf, function(err, doc) {
if (doc._etag == requestEtag) {
// Success - want to update
} else {
// CURRENTLY: Discard the read result I just paid lots of RUs to read
// IDEALLY: check whether response `options` or similar indicates retrieval
was skipped due to doc not being present with that etag anymore
...
// ... Continue with an alternate strategy
}
});
有没有办法将 options
传递给 readDocument
调用,以便回调将被告知“它已更改,因此我们没有按照您的要求获取它” “?
(我真正的问题是除了 the readDocument
undocumentation in the js-server docs 之外找不到任何文档)
最佳答案
从技术上讲,您可以通过创建 responseOptions
对象并将其传递给调用来实现这一点。
function sample(selfLink, requestEtag) {
var collection = getContext().getCollection();
var responseOptions = { accessCondition: { type: "IfMatch", condition: requestEtag } };
var isAccepted = collection.readDocument(selfLink, responseOptions, function(err, doc, options) {
if(err){
throw new Error('Error thrown. Check the status code for PreconditionFailed errors');
}
var response = getContext().getResponse();
response.setBody(doc);
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
但是,即使您提供的 etag 不是文档所具有的 etag,您也不会收到错误,并且可以正确取回文档本身。它只是不应该在存储过程中使用 readDocument
函数来处理。
关于javascript - Azure CosmosDb 存储过程 IfMatch 谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355886/