gpt4 book ai didi

azure - Microsoft.Azure.Documents.BadRequestException : ResourceType Document is unexpected

转载 作者:行者123 更新时间:2023-12-03 04:49:39 28 4
gpt4 key购买 nike

我正在尝试更新数据库中的特定产品。我从正文传递产品值并替换它。这里我的分区键是catlog id。blow我给出了我的所有代码示例,请告诉我我做错了什么?

Json 正文:

{
"id": "9fc41e97-c32e-45e6-853f-ae7993cbc773",
"VendorName": "string",
"Industy": {
"Id": "40f5a6f5-2947-469c-8d29-6aa84de982cb",
"IdustryId": 0,
"IdustryName": "string",
"Category": {
"id": "bf16c9a1-52c6-4e88-b24a-082591718ece",
"Name": "string",
"Description": "string",
"Subcategory": {
"id": "c60a191a-f5b8-4cff-8926-e4b364f8aba2",
"Name": "string",
"Description": "string",
"Product": {
"Id": "97c27069-4a16-44c3-bdf1-627c8ea339fa",
"Name": "string",
"CurrentQuantity": 0,
"Tag": "string",
"Unit": "string",
"Price": 0,
"hasMethodOfPreparation": true,
"Addons": {
"id": "1b7afbc0-36df-4a97-951e-4f0fef0c683f",
"Description": "string",
"Price": 0
},
"MethodOfPreparation": {
"id": "9e1581cf-8401-44c0-ac8b-15e972276178",
"Description": "string",
"Price": 0
}
}
}
}
}

Controller 代码:

  [HttpPut("UpdateProductByIdAsync/{id}", Name = "UpdateProductById")]
public async Task<IActionResult> UpdateProductByIdAsync(
[FromRoute] Guid id,
[FromBody] Product order)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

if (id == null
|| id == Guid.Empty)
{
return BadRequest("Id required.");
}

order.Id = id;

var result =
await _catalogRepository.UpdateProductByIdAsync(
id,
order);

return Ok(result);
}

存储库

  public async Task<Catalog> UpdateProductByIdAsync(
Guid id,
Product order)
{

var requestOptions =
new RequestOptions
{
PartitionKey = new Microsoft.Azure.Documents.PartitionKey(id.ToString())
};


var catalogDocument = await _cosmosClient.ReplaceDocumentAsync(
UriFactory.CreateDocumentCollectionUri(
_azureCosmosDbOptions.Value.DatabaseId, "catalog"), order, requestOptions);

return
(Catalog)((dynamic)catalogDocument.Resource);
}


}

错误:

 <pre class="rawExceptionStackTrace">Microsoft.Azure.Documents.BadRequestException: ResourceType Document is unexpected.&#xD;&#xA;ActivityId: 62f61852-d2a7-466a-9765-80db1d02de32, Windows/10.0.17134 documentdb-netcore-sdk/2.4.0&#xD;&#xA;   at Microsoft.Azure.Documents.PathsHelper.GeneratePathForNameBased(ResourceType resourceType, String resourceFullName, Boolean isFeed, Boolean notRequireValidation)&#xD;&#xA;   at Microsoft.Azure.Documents.GatewayStoreModel.GetEntityUri(DocumentServiceRequest entity)&#xD;&#xA;   at Microsoft.Azure.Documents.GatewayStoreModel.ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken)&#xD;&#xA;   at Microsoft.Azure.Documents.Client.DocumentClient.ProcessRequestAsync(DocumentServiceRequest request, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken)&#xD;&#xA;   at Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentPrivateAsync(String documentLink, Document document, RequestOptions options, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken)&#xD;&#xA;   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)&#xD;&#xA;   at Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException)&#xD;&#xA;   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)&#xD;&#xA;   at Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentInlineAsync(String documentLink, Object document, RequestOptions options, CancellationToken cancellationToken)&#xD;&#xA;   at CatalogAPI.Services.CatalogRepository.UpdateProductByIdAsync(Guid id, Product order) in D:\UCDP\UCDP\UCDP\src\Service\Catalog\CatalogAPI\Services\CatalogRepository.cs:line 322&#xD;&#xA;   at CatalogAPI.Controllers.CatalogController.UpdateProductByIdAsync(Guid id, Product order) in D:\UCDP\UCDP\UCDP\src\Service\Catalog\CatalogAPI\Controllers\CatalogController.cs:line 158&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)&#xD;&#xA;   at System.Threading.Tasks.ValueTask`1.get_Result()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA;   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA;   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)&#xD;&#xA;   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA;   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)&#xD;&#xA;   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</pre>

请帮助我完成这项任务。我尝试了 upsertDocumentAsync,但创建了一个新的文档文件,而不是更新现有的文档文件。

最佳答案

当您调用ReplaceDocumentAsync时,您需要提供Collection Uri

作业应如下所示:

_collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName);

关于azure - Microsoft.Azure.Documents.BadRequestException : ResourceType Document is unexpected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56502687/

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