Return 来自 DocumentDb 中 CreateDocument 上的 PreTrigger 的数据
Return data from PreTrigger on CreateDocument in DocumentDb
预触发看起来像这样:
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?
是否可以 return 计算出的值无需再次查询文档即可响应?
您可以在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"));
}
}
}
预触发看起来像这样:
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?
是否可以 return 计算出的值无需再次查询文档即可响应?
您可以在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"));
}
}
}