0

预触发器看起来像这样:

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?

是否可以将计算值返回到响应而无需再次查询文档?

4

1 回答 1

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"));
        }
    }
}
于 2018-10-24T08:27:57.933 回答