简体   繁体   English

使用JavaScript Azure函数写入Azure Cosmos DB

[英]Write to Azure Cosmos DB using JavaScript Azure Function

I'm following this example. 我正在关注这个例子。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb

Instead of using C# as in the example, I want to use JavaScript. 我不想使用示例中的C#,而是要使用JavaScript。 But I'm trying to figure out how this part is written in JS. 但是我试图弄清楚这部分是如何用JS编写的。

taskDocument = new {
    name = name,
    duedate = duedate.ToString(),
    task = task
};

I tried the above, and below, but both threw an exception. 我尝试了上面和下面的方法,但是都抛出了异常。

taskDocument = new {
    name: name,
    duedate: duedate,
    task: task
};

Guess it is a simple task for those who know how it works. 猜猜对于那些知道它如何工作的人来说这是一个简单的任务。

The proper syntax for object initialization is 对象初始化的正确语法是

let taskDocument = {
    name: name,
    duedate: duedate,
    task: task
};

You then need to assign this value to the output binding, ie to context.bindings.taskDocument . 然后,您需要将此值分配给输出绑定,即context.bindings.taskDocument

Okay, it was just me being stupid. 好吧,只是我很愚蠢。 It should have been like this. 应该是这样的。

taskDocument = {
    name: name,
    duedate: duedate,
    task: task
};

No new in JS :P JS:P中没有new

Update 更新资料

Complete example. 完整的例子。

context.bindings.taskDocument = JSON.stringify({ 
   name: name,
   duedate: duedate,
   task: task
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM