因此,我试图创建一个原型web应用程序的存储过程。我的类/文档名是"Plan“,它有一个Description属性和一个int属性--这些类/文档的实际功能对这个问题并不完全重要,所以让我们忽略它。
数据库名为"PlanDB“,我希望对其执行存储过程的集合称为”计划“,当然,我也保存了存储过程。
这是我的C#应用程序中的函数:
PlanService.cs
public async Task SwapOrderingNumbers(string id1, string id2)
{
try
{
await client.ExecuteStoredProcedureAsync<bool>(UriFactory.CreateStoredProcedureUri("PlanDB", "Plans", "swapOrderNumberSproc"), id1, id2);
}
catch (DocumentClientException de)
{
throw;
}
}正如您所看到的,我有参数id1和id2,我希望在执行上述存储过程时使用它们--理想情况下,它应该围绕着两个计划的OrderingNumber交换。
swapOrderNumberSproc
// Stored procedure for swapping ordering numbers
// @param planId1 - Plan 1's ID
// @param planId2 - Plan 2's ID
var swapOrderNumberSproc = {
id: "swapOrderNumberSproc",
serverScript: function (planId1, planId2) {
var context = getContext();
var collection = context.getCollection();
var response = context.getResponse();
var plan1Document, plan2Document;
// query for Plans
var filterQuery = 'SELECT * FROM Plans a where a.id = "' + planId1 + '"';
var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, {},
function (err, documents, responseOptions) {
if (err) throw new Error("Error" + err.message);
if (documents.length != 1) throw "Unable to find both names";
plan1Document = documents[0];
var filterQuery2 = 'SELECT * FROM Plans a where a.id = "' + planId2 + '"';
var accept2 = collection.queryDocuments(collection.getSelfLink(), filterQuery2, {},
function (err2, documents2, responseOptions2) {
if (err2) throw new Error("Error" + err2.message);
if (documents2.length != 1) throw "Unable to find both names";
plan2Document = documents2[0];
swapOrder(plan1Document, plan2Document);
return;
});
if (!accept2) throw "Unable to read Plan details, abort ";
});
if (!accept) throw "Unable to read Plan details, abort ";
// swap the two Plans’ OrderingNumbers
function swapOrder(plan1, plan2) {
var plan1NumberSave = plan1.OrderingNumber;
plan1.OrderingNumber = plan2.OrderingNumber;
plan2.OrderingNumber = plan1NumberSave;
var accept = collection.replaceDocument(plan1._self, plan1,
function (err, docReplaced) {
if (err) throw "Unable to update Plan 1, abort ";
var accept2 = collection.replaceDocument(plan2._self, plan2,
function (err2, docReplaced2) {
if (err) throw "Unable to update Plan 2, abort"
});
if (!accept2) throw "Unable to update Plan 2, abort";
});
if (!accept) throw "Unable to update Plan 1, abort";
}
}来自C#的C#通过POST调用控制器中的端点"/swapnumbers“:
PlansController
[Route("swapnumbers")]
[HttpPost]
public async Task SwapOrderingNumbers()
{
await activityService.SwapOrderingNumbers("ca35e414-f1b8-49dc-89e7-61e2e100d14a", "dd4a8298-55b8-425b-b16b-f73229399107");
}目前,以参数形式给出的ID被硬编码以简化。
每当我试图通过POST执行存储过程时,它都会返回一个错误500。我做错了什么?
编辑
跟踪
Microsoft.Azure.Documents.BadRequestException: Message: {"Errors":
["Encountered exception while compiling Javascript. Exception =
SyntaxError: Syntax error\r\nSource information: line: 5, column: 1,
source line:\r\nvar swapOrderNumberSproc = {"]}
ActivityId: bf1bacba-0375-4a51-9c94-07c89dfb4868, Request URI:
/apps/DocDbApp/services/DocDbServer19/partitions/a4cb495f-38c8-11e6-
8106-8cdcd42c33be/replicas/1p/
at Microsoft.Azure.Documents.TransportClient.ThrowIfFailed(String
resourceAddress, StoreResponse storeResponse, Uri physicalAddress, Guid
activityId)
at Microsoft.Azure.Documents.RntbdTransportClient.
<InvokeStoreAsync>d__3.MoveNext()我从存储过程中删除的部分
var swapOrderNumberSproc = {
id: "swapOrderNumberSproc",
serverScript: 被取代
function swapOrderNumberSproc(activityId1, activityId2) {发布于 2017-09-29 16:51:05
看来您的SP脚本应该从function swapOrderNumberSproc (planId1, planId2){开始。
function swapOrderNumberSproc(planId1, planId2) {
var context = getContext();
var collection = context.getCollection();
var response = context.getResponse();
var plan1Document, plan2Document;
// query for Plans
var filterQuery = 'SELECT * FROM Plans a where a.id = "' + planId1 + '"';
var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, {},
function (err, documents, responseOptions) {
if (err) throw new Error("Error" + err.message);
if (documents.length != 1) throw "Unable to find both names";
plan1Document = documents[0];
var filterQuery2 = 'SELECT * FROM Plans a where a.id = "' + planId2 + '"';
var accept2 = collection.queryDocuments(collection.getSelfLink(), filterQuery2, {},
function (err2, documents2, responseOptions2) {
if (err2) throw new Error("Error" + err2.message);
if (documents2.length != 1) throw "Unable to find both names";
plan2Document = documents2[0];
swapOrder(plan1Document, plan2Document);
response.setBody(true);
});
if (!accept2) throw "Unable to read Plan details, abort ";
});
if (!accept) throw "Unable to read Plan details, abort ";
// swap the two Plans’ OrderingNumbers
function swapOrder(plan1, plan2) {
var plan1NumberSave = plan1.OrderingNumber;
plan1.OrderingNumber = plan2.OrderingNumber;
plan2.OrderingNumber = plan1NumberSave;
var accept = collection.replaceDocument(plan1._self, plan1,
function (err, docReplaced) {
if (err) throw "Unable to update Plan 1, abort ";
var accept2 = collection.replaceDocument(plan2._self, plan2,
function (err2, docReplaced2) {
if (err) throw "Unable to update Plan 2, abort"
});
if (!accept2) throw "Unable to update Plan 2, abort";
});
if (!accept) throw "Unable to update Plan 1, abort";
}
}而且,您没有在任何时候返回一个bool,您必须在response中发送值。我把你的return;改成了response.setBody(true);。
在此之后,我能够在没有编译错误的情况下从C#运行它并返回一个bool。
https://stackoverflow.com/questions/46443678
复制相似问题