gpt4 book ai didi

azure - Cosmos DB 存储过程未执行

转载 作者:行者123 更新时间:2023-12-03 05:03:53 25 4
gpt4 key购买 nike

我是 Cosmos DB 新手。我试图执行一个存储过程,它只是根据 id 获取所有文档并更新每个文档的一个属性。执行时未能执行。

这个存储过程有什么问题?

string database = ConfigurationManager.AppSettings["database"];
string collection = ConfigurationManager.AppSettings["collection"];
client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);

DocumentCollection obj = await DocumentClientHelper.GetOrCreateCollectionAsync(client, database, collection);
string scriptFileName = @"E:\Satyaray\NoSql\NosqlDemoConsole\NosqlDemoConsole\Updatevalue.js";
string scriptId = Path.GetFileNameWithoutExtension(scriptFileName);

var sproc = new StoredProcedure {
Id = scriptId,
Body = File.ReadAllText(scriptFileName)
};

await DocumentClientHelper.TryDeleteStoredProcedure(client, obj, sproc.Id);

sproc = await client.CreateStoredProcedureAsync(obj.SelfLink, sproc);

var response = await client.ExecuteStoredProcedureAsync < string > (sproc.SelfLink, new RequestOptions {
PartitionKey = new PartitionKey("XMS-0001")
}, "XMS-001-FE24C");

存储过程

function simple(id) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();

getAndUpdatedata();

function getAndUpdatedata() {

var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r where r.id=' + id,
function (err, feed, options) {
if (err) throw err;

for (var i = 0; i < feed.length; i++) {
var metaDoc = feed[i];

metaDoc.readingTime = new Date();
var isAccepted = collection.replaceDocument(metaDoc._self, metaDoc, function (err) {
if (err) throw err;

});
if (!isAccepted) throw new Error("The call replaceDocument(metaDoc) returned false.");
}


});

if (!isAccepted) throw new Error("The call queryDocuments for metaDoc returned false.");
}
}

错误消息

Message: {"Errors":["Encountered exception while executing function. Exception = Error: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":32,\"end\":35},\"code\":\"SC2001\",\"message\":\"Identifier 'XMS' could not be resolved.\"},{\"severity\":\"Error\",\"location\":{\"start\":40,\"end\":45},\"code\":\"SC2001\",\"message\":\"Identifier 'FE24C' could not be resolved.\"}]}\r\nStack trace: Error: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":32,\"end\":35},\"code\":\"SC2001\",\"message\":\"Identifier 'XMS' could not be resolved.\"},{\"severity\":\"Error\",\"location\":{"]}
ActivityId: 62e49cf4-1259-4d36-a196-7f752ceeba53, Request URI: /apps/DocDbApp/services/DocDbServer22/partitions/a4cb4962-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.20.108.4, documentdb-dotnet-sdk/1.20.2 Host/32-bit MicrosoftWindowsNT/6.2.9200.0

最佳答案

我使用console.log打印你的sql,我发现它看起来像:

SELECT * FROM root r where r.id= XMS-001-FE24C

实际上,应该是这样的:

SELECT * FROM root r where r.id= 'XMS-001-FE24C'

所以,请稍微修改一下存储过程中的 SQL,它就可以正常工作:

var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
"SELECT * FROM r where r.id = '" + id + "'",
......

顺便说一句,id是用户定义的提到的资源的唯一名称here 。所以我认为你的sql结果应该只包含一条数据,不需要循环feed数组。

希望对您有帮助。

关于azure - Cosmos DB 存储过程未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com