我是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
答案 0 :(得分:2)
我使用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 + "'",
......
BTW,id
是用户定义的资源的唯一名称,提到here。所以我认为你的sql的结果应该只包含一个数据,不需要循环feed
数组。
希望它对你有所帮助。