aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

[JS] Comparison of numbers from dynamodb response is wrong #344

Closed LucasVera closed 8 months ago

LucasVera commented 10 months ago

Issue

Hi There! Currently ran into an issue when trying to compare two numbers in an appsync js resolver. Both these numbers come from two number fields from dynamodb, and after doing a GetItem operation in the request, I try to compare them in the response, however the logic seems to be reversed (also happened to me with a query operation, however didn't take evidence of that)

Dynamodb data

This is the dynamodb data being fetched: image

{
 "id": "1",
 "num1": 40,
 "num2": 40
}

Resolver code

The resolver code is the following:

import { util } from '@aws-appsync/utils';
export function request (ctx) {
  return { operation: 'GetItem', key: util.dynamodb.toMapValues({ id: "1" })};
}

export function response (ctx) {
  const { result } = ctx;

  console.log('dynamodb result:', result)

  const num1 = result.num1; // 40 in dynamodb
  const num2 = result.num2; // 40 in dynamodb

  console.log('num1 value', num1)
  console.log('num1 type', typeof num1)
  console.log('num2 value', num2)
  console.log('num2 type', typeof num2)
  console.log('this should be true -->', num1 === num2);
  console.log('this should be false -->', num1 !== num2);

  return {};
}

Cloudwatch logs

After executing, the results in cloudwatch are the following: image

Notice that the logs show the value of num1 and num2 to be 40, and the types are both number, however the comparison logs are reversed.

Workaround

Curiously enough, after adding a + 0 to num1 and num2, the comparisons would start working again:

Updated resolver code with the workaround

import { util } from '@aws-appsync/utils';
export function request (ctx) {
  return { operation: 'GetItem', key: util.dynamodb.toMapValues({ id: "1" })};
}

export function response (ctx) {
  const { result } = ctx;

  console.log('dynamodb result:', result)

  const num1 = result.num1 + 0; // 40 in dynamodb
  const num2 = result.num2 + 0; // 40 in dynamodb

  console.log('num1 value', num1)
  console.log('num1 type', typeof num1)
  console.log('num2 value', num2)
  console.log('num2 type', typeof num2)
  console.log('this should be true -->', num1 === num2);
  console.log('this should be false -->', num1 !== num2);

  return {};
}

Cloudwatch logs with workaround

image

gilbertw1 commented 10 months ago

Thanks for the detailed report. We'll look into this.

gilbertw1 commented 8 months ago

This issue has been resolved, please let us know if you run into any further problems.