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:
{
"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:
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 {};
}
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 aquery
operation, however didn't take evidence of that)Dynamodb data
This is the dynamodb data being fetched:
Resolver code
The resolver code is the following:
Cloudwatch logs
After executing, the results in cloudwatch are the following:
Notice that the logs show the value of
num1
andnum2
to be40
, and the types are bothnumber
, however the comparison logs are reversed.Workaround
Curiously enough, after adding a
+ 0
tonum1
andnum2
, the comparisons would start working again:Updated resolver code with the workaround
Cloudwatch logs with workaround