Возврат данных из PreTrigger в CreateDocument в DocumentDb - PullRequest
0 голосов
/ 23 октября 2018

Предварительный триггер выглядит примерно так:

function calculate() {
    var context = getContext();
    var request = context.getRequest();

    var documentToCreate = request.getBody();
    documentToCreate["OrderNumber"] = 1;
    request.setBody(documentToCreate);
} 

Я создаю документ с помощью API клиента C # с PreTriggerInclude:

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?

Можно ли вернуть вычисленное значение вответ без необходимости повторного запроса документа?

1 Ответ

0 голосов
/ 24 октября 2018

Вы можете получить созданный документ в ответ на метод 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"));
        }
    }
}
...