javascript - 如何使用 Azure 函数和 HTTP 触发器 POST 更新 Cosmos Azure (NOSQL) 中的数据

标签 javascript node.js azure azure-cosmosdb azure-functions

我是 Azure 新手,我正在尝试使用 Azure 函数来触发 NodeJS, 我已经在 Azure Cosmos 上拥有一个 NoSQL 数据库。 示例:

{
   ...
   "shop":{
      "fruits":[
         "orange",
         "strawberry",
         "lemon"
      ],
      "clothes":[
         "man",
         "woman",
         "babies"
      ]
   }
   ...
}

然后我想将一个名为apple的新水果添加到fruits数组中,或者从衣服中删除babies>。还将ma​​n更新为men, 我能怎么做 ? 我找到了context.bindings。但我还不知道怎么用 有人可以帮助我吗?

非常感谢。

最佳答案

这是一个示例函数,它递增文档的 num 字段。

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "HttpTriggerJSUpdateDocument/{docid}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "MyDB",
      "collectionName": "MyCollection",
      "id": "{docid}",
      "connection": "mydocdb_DOCUMENTDB",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "MyDB",
      "collectionName": "MyCollection",
      "createIfNotExists": false,
      "connection": "mydocdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

index.js:

module.exports = function (context, req) {
    let inputDocument = context.bindings.inputDocument;
    context.log('JavaScript HTTP trigger, current value: ' + 
        (inputDocument && inputDocument.num));

    inputDocument.num = inputDocument.num + 1;

    context.bindings.outputDocument = inputDocument;

    context.res = {
        body: 'Result is ' + inputDocument.num
    };
    context.done();
};

关于javascript - 如何使用 Azure 函数和 HTTP 触发器 POST 更新 Cosmos Azure (NOSQL) 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951972/

相关文章:

asp.net-mvc - Azure FileShare 有什么好处?将文件存储在 Azure 中与将文件存储在 Blob 存储中是否是一个好的解决方案

javascript - 如何使用http 2实现cookie jar

Javascript 变量到 PHP 变量,无需重新加载页面

javascript - 如何从文本文件读取数据、更改数据并将其附加到 html 中?

javascript - react-native onPress 克隆元素

azure - 碎片恢复管理

node.js - 错误: Timed out while waiting for handshake in Lambda logs

node.js - 在nodejs中访问硬件信息

javascript - 案例功能不适用于不和谐机器人

node.js - 在 Azure Web 服务上部署 Node JS 应用程序