预触发看起来像这样:
function calculate() {
var context = getContext();
var request = context.getRequest();
var documentToCreate = request.getBody();
documentToCreate["OrderNumber"] = 1;
request.setBody(documentToCreate);
}
我使用带有PreTriggerInclude的C#客户端api创建文档
var doc = new MyDocument
{
Title = "My Document 1",
//OrderNumber - calculated by PreTrigger
};
var result = await client.CreateDocumentAsync(eventCollectionUri, doc,
new RequestOptions { PreTriggerInclude = new List<string> { "CalculateOrderNumber" }});
// how to get OrderNumber here?
是否可以将计算出的值返回到响应而不必再次查询文档?
答案 0 :(得分:0)
您可以通过CreateDocumentAsync
方法的响应来获得创建的文档,只需使用result.Resource.GetPropertyValue<String>("OrderNumber")
。
示例代码:
using JayGongDocumentDB.pojo;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JayGongDocumentDB.module
{
class TestTrigger
{
private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
private static readonly string authorizationKey = "***";
private static readonly string databaseId = "db";
private static readonly string collectionId = "coll";
private static DocumentClient client;
public static async Task TestTriggerAsync()
{
client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
var doc = new Document
{
Id = "My Document 1",
//OrderNumber - calculated by PreTrigger
};
var result = await client.CreateDocumentAsync("dbs/db/colls/coll", doc,
new RequestOptions { PreTriggerInclude = new List<string> { "calculate" } });
Console.WriteLine(result.Resource.GetPropertyValue<String>("OrderNumber"));
}
}
}
希望它对您有帮助。