我正在尝试将数据从azure javascript函数应用程序插入到cosmos DB。 这是 function.json 文件
{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "CosmosDBConnection",
"databaseName": "roi",
"collectionName": "reports",
"createLeaseCollectionIfNotExists": "true"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
index.js 有这样的代码。
module.exports = async function (context, documents) {
if (!!documents && documents.length > 0) {
context.log('Document Id: ', documents[0].id);
}
}
是否有任何函数或查询来插入数据?来自 api 的请求数据将如下所示。(它来自请求的 rawBody 属性)
当我获取数据时,它工作正常。
{
"name": "inputDocumentIn",
"type": "cosmosDB",
"databaseName": "roi",
"collectionName": "reports",
"sqlQuery": "SELECT * from reports r",
"connectionStringSetting": "CosmosDBConnection",
"direction": "in"
}
local.setting.json 文件
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"CosmosDBConnection": "AccountEndpoint=.....;"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
}
}
最佳答案
请检查连接字符串是否指向正确的帐户,并检查是否是由防火墙和网络引起的。转到 Azure 门户上的 Cosmos 数据库帐户,然后单击“防火墙和虚拟网络”。您可以选择“所有网络”或“选定网络”并填写您当前的IP。
关于azure - 如何使用azure JavaScript函数应用程序在cosmos DB中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59173802/