Here is the code, I am trying to check the result of insert statement. If it comes null(if tag title already exists), it has to execute else statement. But it is failing in else statement throwing lost connection error.
 return this.store.tx('create-tags', async (transaction: any) => {
    tagDetails.forEach((tag: any) => {
     transaction
      .oneOrNone(
      `INSERT INTO tag(title)
         VALUES ($1)
         ON CONFLICT DO NOTHING
         RETURNING tag_id, title`,
        [tag.title],
      )
        .then((result: any) => {
            console.log('the tag details are', result);
           if (result !== null) {
              this.createTags(collectionId, tag.item_id, result.tag_id);
            } else {
           transaction.oneOrNone(
           `
           SELECT tag_id
           FROM tag
           WHERE title = $1
           `,
           [tag.title],
              ).then((tagId: string) => {
               console.log('the tagid in else statement is', tagId);
              if (tagId) {
                this.createTags(collectionId, tag.item_id, tagId);
               }
             })
             .catch(err => {
               console.log('the error in else statement is', err);
             });
       }
      });
  });
 
     
    