Query Issue - works sporadically

I’ve had tables and server for a while and it’s been working great: save/query.

However lately I’ve noticed things running a bit slow and today as I was working on features I noticed that queries are super unpredictable. Sometimes they work and sometimes they don’t without any change to parameters. So, I’m not quite sure what’s going on but I figured I bring it up.

I’ve tried searching with and without params and it really doesn’t matter.

Any thoughts, suggestions, help is super appreciated.

Hi sounds strange, can you post the query and the error messages you are getting?


I have a table named “OffchainSales” and input goes like this:

async function salePending(id) {
  const OffchainSales = Moralis.Object.extend('OffchainSales')
  const sales = new OffchainSales()
  let tokenId = id.toString()
  sales.set('tokenId', tokenId)
  sales.set('status', 'pending')
  try {
    await sales.save()
    console.log('sale saved', sales)
  } catch (error) {

Table input creates table and entry:

To query the table, I have the following:

async function queryPendingSale(id) {
  let tokenId = web3.utils.hexToNumberString(id)
  const query = new Moralis.Query('OffchainSales')
  query.equalTo('tokenId', tokenId)
  const results = await query.find()
  return results[0].attributes.status

Out of 10 queries it worked about 3 times and 7 times I get the following error in console:

Uncaught (in promise) TypeError: Cannot read property 'attributes' of undefined

When I remove search params (search by id, etc), I get this in console

sale status [ParseObject]

and sometimes I only get

sale status []

When it works, it clearly returns the status inside the table, which is awesome.

Thank you for your help and feedback.

I had similar issue when trying to query for data. I managed to fix it by removing query.equalTo function. Not sure if it works for you but that fixes the problem for me

Tried removed equalTo and it seems to work every time. For the moment when I only have 1 ID it works, but this will need to be ID specific so at some point I would need it to work. Thanks for the help. Perhaps that’s where the problem is “equalTo”. And the weird part is that it sometimes works with “equalTo”

Hey @nev
Could you send your server domain?
Thanks :blush:

Hi Nev! Hope you had a good weekend.

I tried running the code above directly in the dashboard console. I commented out the hex to number as that is not available in the dashboard (and need to use Parse keyword instead of Moralis). When I do this and pass a number as the id the query succeeds every time for me… so perhaps the error has to do with the hex conversion?

Also if you only care about the first result you can use query.first() instead of query.find().

async function queryPendingSale(id) {
  const tokenId = String(id);
  // let tokenId = web3.utils.hexToNumberString(id)
  const query = new Parse.Query('OffchainSales')
  query.equalTo('tokenId', tokenId)
  const results = await query.first()
  return results[0].attributes.status

await salePending(1);
console.log(await queryPendingSale(1));

sorry for delay and thanks for your super speedy response. i will absolutely follow your instructions