gpt4 book ai didi

azure - 通过azure函数更新和删除cosmos db中的文档

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

我是 cosmos db 和 azure 函数的新手,但我一无所获。我已经能够找到所有关于创建和读取文档但无法更新和删除的教程。似乎没有人有使用 azure 函数的完整 CRUD 教程。

有人可以向我展示 azure 函数中的典型 .csx 文件,该文件接收文档、更新它并返回 OK 响应吗?

我已经尝试过了

#load "..\Shared\Classes.csx"
using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req,
IEnumerable<Business> businessToBeUpdated, out dynamic updatedBusiness,
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

//compiler requires this assignment
updatedBusiness = null;

// Get request body
Business data = req.Content.ReadAsAsync<Business>().Result;
businessToBeUpdated = businessToBeUpdated.FirstOrDefault<Business>();
log.Info(businessToBeUpdated.Count().ToString());
if(businessToBeUpdated != null && data != null)
{
//update it
businessToBeUpdated = data;
//updatedBusiness.id = data.id;
log.Info(businessToBeUpdated.website);
}
else{
return req.CreateResponse(HttpStatusCode.BadRequest);
}

return req.CreateResponse(HttpStatusCode.OK);
}

这是与其关联的绑定(bind)。

{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "updatebiz/{id}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "businessToBeUpdated",
"databaseName": "dbname",
"collectionName": "Businesses",
"sqlQuery": "Select * FROM c where c.id = {id}",
"connection": "connection",
"direction": "in"
},
{
"type": "documentDB",
"name": "updatedBusiness",
"databaseName": "dbname",
"collectionName": "Businesses",
"createIfNotExists": false,
"connection": "connection",
"direction": "out"
}
],
"disabled": false
}

最佳答案

更新文档的函数的最简单示例看起来与创建文档的函数完全相同:函数将根据指定 id 的文档是否已存在执行其中一项操作。

您没有提及您面临的具体问题。您的代码甚至无法编译,将可枚举对象和单个对象相互分配。最重要的是,您永远不会将 updatedBusiness 分配给 null 以外的任何内容。

我提供了一个工作示例,它可以完成我认为您想要完成的任务。

csx 脚本:

using System.Net;

public class Business
{
public string id { get; set;}
public string name { get; set;}
}

public static HttpResponseMessage Run(HttpRequestMessage req,
Business businessToBeUpdated, out Business updatedBusiness, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

var data = req.Content.ReadAsAsync<Business>().Result;
if(businessToBeUpdated == null || data == null)
{
updatedBusiness = businessToBeUpdated;
return req.CreateResponse(HttpStatusCode.BadRequest);
}

updatedBusiness = data;
// or merge data and businessToBeUpdated in some desired way

return req.CreateResponse(HttpStatusCode.OK);
}

function.json:

{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "updatebiz/{id}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "businessToBeUpdated",
"databaseName": "dbname",
"collectionName": "Businesses",
"id": "{id}",
"connection": "connection",
"direction": "in"
},
{
"type": "documentDB",
"name": "updatedBusiness",
"databaseName": "dbname",
"collectionName": "Businesses",
"id": "{id}",
"connection": "connection",
"direction": "out"
}
],
"disabled": false
}

关于azure - 通过azure函数更新和删除cosmos db中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48024033/

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