1

I have the following two functions one calling the other but the record variable is undefined followed by errors. I can't figure out why the script doesn't wait. It seems to just proceed with the undefined variable.

async function searchRecord(recordID) {
    client.search({
        index: 'records',
        type: 'record',
        body: {
            query: { match: { _id: recordID } }
        }
    }).then(result => {
        return result
    }).catch(error => {
        console.log(error)
        return []
    })
}

function test(jsonRecord) {
    const userID = jsonRecord.users[0]
    searchRecord(jsonRecord.objectID).then(record => {
        if (record.length === 0) {
            record = jsonRecord
        }
    })
}

The error that I get is: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
Michael
  • 1,759
  • 4
  • 19
  • 29

4 Answers4

2

This is asynchronous, try using await.

async function searchRecord(recordID) {
  try {
    const result = await client.search({
      index: 'records',
      type: 'record',
      body: {
        query: {
          match: { _id: recordID }
        }
      }
    });
    return result;
  } catch (error) {
    console.log(error);
    return [];
  }
}
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Deepak Gupta
  • 614
  • 1
  • 7
  • 14
1

Try updating searchRecord to return:

async function searchRecord(recordID) {
  return client
    .search({
      index: "records",
      type: "record",
      body: {
        query: {
          match: { _id: recordID },
        },
      },
    })
    .then((result) => {
      return result;
    })
    .catch((error) => {
      console.log(error);
      return [];
    });
}
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Most solutions seemed to work but this was by far the easiest to implement. Strange that the top return is needed here to make this work but I guess the .then and .catch all return the values to the client . – Michael Sep 05 '20 at 20:38
0

The function client.search() returns a promise. You could choose to return that promise as is from searchRecord(). And then, handle the catch in your test() function.

Alternatively you could also handle the error inside searchRecord() as well by implementing a try catch block. But the key in this case is to wait for client.search() to finish before returning from searchRecord().

function searchRecord(recordID) {
    return client.search({
        index: 'records',
        type: 'record',
        body: {
            query: { match: { _id: recordID } }
        }
    });
}

function test(jsonRecord) {
    const userID = jsonRecord.users[0]
    searchRecord(jsonRecord.objectID).then(record => {
        if (record.length === 0) {
            record = jsonRecord
        }
    }).catch(error => {
        console.log(error)
        return []
    })
}

The error that I get is: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined

The reason for this is that searchRecord() returns promise that immediately resolves to undefined. There is no return statement in the function searchRecord().

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
-1

why you not using Promise? it's ok if you want to use async-await like answers above but using Promise its became very easy

function searchRecord (recordID) {
  return new Promise((resolve, reject)=>{
    client.search({
      index: 'records',
      type: 'record',
      body: {
        query: {
          match: { _id: recordID }
        }
      }
    }).then(
      result => resolve(result)
    ).catch(
      error => {console.log(error);reject());
  });
}


function test (jsonRecord) {
    const userID = jsonRecord.users[0]
    searchRecord(jsonRecord.objectID)
      .then(
        record => {
          if (record.length === 0) {
            record = jsonRecord
          }
        }
      )
  }