DB Query - Match doesn't match string variable

Hello,

When I use “hard coded string value” query was matched. But same value with “string variable” query was not match. When i preview my string variable in debug mode, it looks single quote was inserted around the string variable.

works ----> query.equalTo(“fromWallet”, “0x434464575765868677980780780078089Ba3c5”);
doesn’t work ----> query.equalTo(“toWallet”, to); // to equals 0x434464575765868677980780780078089Ba3c5

How can i handle this issue.

Thank you in advance

where do you use this syntax? in front end?

can you paste a minimal complete example that replicates this issue?

const query = getQuery();
  const pipeline = [
    {
      group: {
        objectId: "$fromWallet",
        total: { $sum: "$amount" },
      },
    },
  ];
  query.equalTo("fromWallet", from);
  let total;
  await query.aggregate(pipeline).then((results) => {
    total =
      results[0] !== undefined && results[0] !== null ? results[0].total : 0;
  });

you can check here how to paste code on forum:

it makes it easier to be read and copied after that

ok, i read how to paste code on forum and fixed

now, i hope you can help me about the issue

1 Like

maybe try to add some logging,
this works fine for me:

ethAddress = "0x434464575765868677980780780078089Ba3c5"
      query = new Moralis.Query("User");
      result = query.find()
      query.equalTo('ethAddress', ethAddress)
      result.then(
        function(value) {console.log(value);},
        function(error) {console.log(error);}
      ); 
    

can you change “0x434464575765868677980780780078089Ba3c5” with single quote ‘0x434464575765868677980780780078089Ba3c5’ and try again to see that still works fine?

Maybe you can force it to lower case, using the example @cryptokid shared above, you can have such

  const ethAddress = "0x434464575765868677980780780078089Ba3c5";
  const query = new Moralis.Query("User");
  query.equalTo("ethAddress", ethAddress.toLowerCase());
  query.first({ useMasterKey: true }).then(
    function(value) {console.log(value);},
    function(error) {console.log(error);}
  ); 

tried lowercase as you @qudusayo told, but same issue still exist

@cryptokid can you change “0x434464575765868677980780780078089Ba3c5” with single quote ‘0x434464575765868677980780780078089Ba3c5’ and try again to see that still works fine?

Are you querying from the client or server or cloud functions, as it might not working doing it from the client depending on the current user

if i use double quote it works
if i use single quote it doesn’t work

The issue is that single quote was inserted around the string variable.

A single quote or double quote shouldn’t be the problem because they are equal ( === ) in javascript

1 Like

@qudusayo if query works on ‘javascript’ you re right, single quote and double compare to each others value and type are same.

But query works on mongodb.

@cryptokid please just execute your codes that already works fine with single quote

my mode works the same with single quote too, I tried it in the browser console in a browser