gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:55:55 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com