Cannot add Json Object into Azure Cosmos DB with using LogicApps action - azure-cosmosdb

I'm trying to adding Json Object into Azure Cosmos DB with using LogicApps Azure Cosmos DB Action (Patch an Item)
When I try to adding it(Json Object) with Serialized string, it works but when I try to adding as Json Object directly, Exception has occurred like below :
[2022-05-05T18:40:58.485Z] Error message: correlationId='36c1252c-9b2f-452c-8b86-026144567708', operationName='/serviceProviders/AzureCosmosDB.CosmosDBServiceOperationsProvider.HandleException', message='Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.', exception='System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.
[2022-05-05T18:40:58.488Z] at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
[2022-05-05T18:40:58.490Z] at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable1 value) [2022-05-05T18:40:58.493Z] at Microsoft.Azure.Workflows.ServiceProviders.CosmosDB.Utilities.CosmosDBUtilities.GetPatchOperations(InsensitiveDictionary1 parameters, String serviceId, CosmosDBOperationType operationType)
[2022-05-05T18:40:58.496Z] at Microsoft.Azure.Workflows.ServiceProviders.CosmosDB.Operations.PatchItemOperation.ExecuteAsync(Container container, String serviceId, InsensitiveDictionary1 parameters) [2022-05-05T18:40:58.498Z] at Microsoft.Azure.Workflows.ServiceProviders.CosmosDB.Providers.CosmosDBServiceOperationsProvider.InvokeOperation(String operationId, InsensitiveDictionary1 connectionParameters, ServiceOperationRequest serviceOperationRequest)
[2022-05-05T18:40:58.500Z] at Microsoft.Azure.Workflows.ServiceProviders.CosmosDB.Providers.CosmosDBServiceOperationsProvider.InvokeOperation(String operationId, InsensitiveDictionary`1 connectionParameters, ServiceOperationRequest serviceOperationRequest)', organizationId='', activityVector='IN.04', additionalProperties='', extensionVersion='1.0.0.0', siteName='UNDEFINED_SITE_NAME', slotName='', activityId='471d1082-cf81-4065-9783-28c61319e7c9'.
below is the Cosmos DB action for patchOperations :
"parameters": {
"databaseId": "messages",
"containerId": "espcreation",
"itemId": "#body('Parse_JSON')?['id']",
"partitionKey": "#body('Parse_JSON')?['recipient']",
"patchOperations": [
{
"type": "Set",
"path": "/currentstatus",
"value": "#body('Parse_JSON')?['type']"
},
{
"type": "Add",
"path": "/steps/-",
"value": "#body('Parse_JSON_2')"
}
]
},
I need to add Json object into array field in Azure Cosmos DB but I stuck on this issue.
Is there anybody can show me how to figure it out?
thanks,
-DB

Related

Facing issue while trying to run the updateitem in dynamo db

I am able to fetch the record from dynamo db and view the response successfully. I need to modify the fetched 'ACCOUNTNAME' attribute in the 'items' array and update the json and also update in dynamo db. Now when I try to update the fetched records I end up with the Invalid attribute value type exception.
I was trying to update it using the key with Array of Strings which is provided with code snippet also tried to update inside for loop using the individual string but both failed with same exception as
"statusCode": 400,
"body": {
"message": "Invalid attribute value type",
"error": {
"errorMessage": "ValidationException"
}
}
I tried to create params and update the call inside the for loop by setting the key as below,
Key: {
"UUID": {
"S": usersOfAccountFromDB.body.Items[key].UUID
}
,
"TYPE": {
"S": user
}
}
but also failed with the same exception.
Fetched Json from dynamo db
[
{
"DEFINITION": "914ba44a-8c26-4b60-af0f-96b6aa37efe6",
"UUID": "830a49cb-4ed3-41ae-b111-56714a71ab98",
"TYPE": "USER",
"RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
"ACCOUNTNAME": "Wolff LLC"
},
{
"DEFINITION": "1f60fded-323d-40e1-a7f8-e2d053b0bed0",
"UUID": "47db3bbe-53ac-4e58-a378-f42331141997",
"TYPE": "USER",
"RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
"ACCOUNTNAME": "Wolff LLC"
},
{
"DEFINITION": "05ddccba-2b6d-46bd-9db4-7b897ebe16ca",
"UUID": "e7290457-db77-48fc-bd1a-7056bfce8fab",
"TYPE": "USER",
"RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
"ACCOUNTNAME": "Wolff LLC"
},
.
.
.
.]
Now I tried to iterate the Json and setup UUID which is the key as the String array as below,
var userUUIDArray : string[] = [];
for (let key in usersOfAccountFromDB.body.Items) {
userUUIDArray.push(usersOfAccountFromDB.body.Items[key].UUID);
}
for (var uuid of userUUIDArray) {
console.log("UUID : " +uuid); // prints all the uuid
}
// Creating a parameter for the update dynamo db
var params = {
TableName: <tableName>,
Key: {
"UUID": {
"SS": userUUIDArray
}
,
"TYPE": {
"S": user
}
},
UpdateExpression: 'SET #ACCOUNTNAME = :val1',
ExpressionAttributeNames: {
'#ACCOUNTNAME': 'ACCOUNTNAME' //COLUMN NAME
},
ExpressionAttributeValues: {
':val1': newAccountName
},
ReturnValues: 'UPDATED_NEW',
};
//call the update of dynamodb
const result = await this.getDocClient().update(param).promise();
I get the error as below,
"body": {
"message": "Invalid attribute value type",
"error": {
"errorMessage": "ValidationException"
}
}
All the approaches failed with same above exception
The update operation which your code currently uses only allow a single item to be updated.
IIUC, you want to update multiple items with one API call. For this you need to use batchWrite operation. Keep in mind that you cannot update more than 25 items per invocation.
The origin of the error you are getting
Your code fails due to the use of "SS" in the UUID field. This field is of type string so you must use "S". Note however that since you're using the document client API you do not need to pass values using this notation. See this answer for further details.
I have resolved the issue now by running the update statement one by one using loop
for (let key in usersOfAccountFromDB.body.Items) {
var updateParam = {
TableName: process.env.AWS_DYNAMO_TABLE,
Key: {
UUID: usersOfAccountFromDB.body.Items[key].UUID,
TYPE: user
},
UpdateExpression: "SET #ACCOUNTNAME = :val1",
ExpressionAttributeNames: {
'#ACCOUNTNAME': 'ACCOUNTNAME'
},
ExpressionAttributeValues: {
":val1": newAccountName
},
ReturnValues: "UPDATED_NEW",
};
const result = await this.getDocClient().update(updateParam).promise();
}

The value 'AddOrder' is not valid

I created a catalog api using asp.net core and cosmos db .I post a catalog data set. but i get following error.I debug it using break points the problem is my result body is null.
{"errors":{"id":["The value 'AddOrder' is not valid."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80000011-0005-fe00-b63f-84710c7967bb"}
Catalog controller code example
Catalog repository code example
azure cosmos db structure :
What am i doing wrong ??
post json file :
{
"Id": 1,
"venderId": 1,
"IndustryName": "dsa",
"ProductName": "sad",
"CurrentQuantity": 1,
"Tag": "sad",
"Unit": "dsad",
"Price": 34,
"Class": "dsfs",
"category": {
"Id": 1,
"Name": "freg",
"Description": "fdf",
"subcategory": [
{
"Id": 1,
"Name": "ergt",
"Description": "erfwef"
}
]
}
}
Your controller code is validating the Model and returning the BadRequest: https://github.com/kajasumanie/calalogapi/blob/master/CalalogAPI/Controllers/BooksController.cs#L41
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
The error you are seeing is unrelated to Cosmos DB, whatever payload you are sending is not valid for the Catalog class.
That class is not available in your repo, so it's hard to say. You should check the payload versus the model class and verify the format and requirements are met.
I indicate the Id as Guid value. When try to add the order, I gave the id value as 1,2,3....
But Guid value I need to pass the Id value like 80000011-0005-fe00-b63f-84710c7967bb.
That is the problem .

AWS AppSync GraphQL query a record by a field value

I have an user table, which consists of email, phone etc., and I would like to query a record based on its email or phone value (instead of #Id). Having not-adequate knowledge to do this - I wrote a schema like this:
type Query {
...
getUser(id: ID!): User
getUserByEmail(input: GetUserByEmailInput!): User
...
}
input GetUserByEmailInput {
email: String!
}
In resolver against getUserByEmail(..), I tried to experiment but nothing worked so far, so its remain to default state:
So when I ran a query like this to the Queries console:
query GetUserByEmail {
getUserByEmail(input: {email: "email#email.com"}) {
id
name
email
image
}
}
this returns an error like this:
{
"data": {
"getUserByEmail": null
},
"errors": [
{
"path": [
"getUserByEmail"
],
"data": null,
"errorType": "DynamoDB:AmazonDynamoDBException",
"errorInfo": null,
"locations": [
{
"line": 41,
"column": 5,
"sourceName": null
}
],
"message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: xxx)"
}
]
}
How can I query a record by non-Id field value?
If you use the Create Resources flow in the console, it will create a listUsers query that looks like the following for example. Note that the DynamoDb operation will be a Scan that has a DynamoDb filter expression where you can use any field to query DynamoDb. See below for the mapping template.
{
"version": "2017-02-28",
"operation": "Scan",
"filter": #if($context.args.filter) $util.transform.toDynamoDBFilterExpression($ctx.args.filter) #else null #end,
"limit": $util.defaultIfNull($ctx.args.limit, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)),
}
You can find more details about Scans and filter expressions in the AWS AppSync documentation:
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-resolvers.html

DynamoDb documentClient.update or delete StringSet throws ValidationException

I successfully update and delete an item from a StringSet in a dynamoDb table when called from my test app running on localhost.
I then upload the app to LightSail but now when I call the same function to update or delete an item it throws a ValidationException!:
{
"message": "Invalid UpdateExpression: Incorrect operand type for operator or
function; operator: DELETE, operand type: MAP",
"code": "ValidationException",
"time": "2018-01-03T13:20:14.919Z",
"requestId": "9HCQMH5RAUBRK1K7BNESNBUD5BVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 10.381373865940402
}
Why? I have not made any changes to my code so why does this happen and how to solve it?
Here's the relevant code:
var documentClient = getDocumentClient();
var paramsSET = {
ExpressionAttributeNames:
{
"#StringSet": "Packages"
},
ExpressionAttributeValues:
{
":value": documentClient.createSet(['filler as SET cannot be empty',
app.packageName
])
},
Key:
{
"EmailAddress": app.emailAddress
},
ReturnValues: "ALL_NEW",
TableName: "Developers",
UpdateExpression: "ADD #StringSet :value"
// UpdateExpression: "DELETE #StringSet :value" ------ to delete value
};
// adds packagename to Packages SET in developers table - creates set if not exist
documentClient.update(paramsSET, function (err, data){}
I could not get it to work using the documentclient api.
finally used the old api and got it to work using dynamodb.updateItem see docs here
still have no idea why it works on localhost (accessing the same dynamodb tables) and not when live on LightSail!

No HTTP resource was found that matches the request URI (...) for all tables

Following this tutorial as a guide (OData/EntityFramework/Asp.Net).
I'm able to execute a simple GET command on the root.
{
"#odata.context": "http://localhost:49624/$metadata",
"value": [
{
"name": "Appointments",
"kind": "EntitySet",
"url": "Appointments"
},
......
{
"name": "Clients",
"kind": "EntitySet",
"url": "Clients"
}
]
}
But anything more complex than that gives me an error message. (I'm using a null routePrefix.)
http://localhost:49624/Services
Gives me:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:49624/Services'.",
"MessageDetail": "No type was found that matches the controller named 'Services'."
}
Here's my super simple GET
[EnableQuery]
public IQueryable<Service> Get()
{
return db.Services;
}
If it matters I'm using Postman to test these commands. Although I imagine that is a non-factor.
I have a database & a DbSet for every table. I have no idea why I can't access any of this.
WebApiConfig:
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Appointment>("Appointments");
builder.EntitySet<Service>("Services");
builder.EntitySet<Employee>("Employees");
builder.EntitySet<Client>("Clients");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
I'm sorry if this is a basic question but I'm really new to all this and have been at this wall too long already haha.
Jan Hommes above pointed out above that the controller class needs to be pluralized (In my case ServiceController -> ServicesController)

Resources