0
votes

I have a graphql endpoint that I'm running a query against, and I'm building my resolver that is massaging the data before returning to the client. My query is this:

   query getTransactions($transID: String!, $confidence: Float) {
     transactions(parentID: $transID, confidence: $confidence) {
       id
       childrens {
         id
         name
         email
         phone
         age
         connectionInfo {
           type
           confidence
         }
       }
       name
       email
       phone
       age
     }
   }

and my resolver is currently looking like this:

const getTransactions = (args: any): any => {
  const { parentID, confidence } = args;
  const trxs = transactions.filter(t => {
    return t.id === parentID;
  });
  let finalChildrens: any[] = [];
  trxs.forEach(t => {
    finalChildrens.concat(filteredChildren(t));
  });
  trxs.concat(finalChildrens);
  return trxs;
};

const filteredChildren = (t: any): any[] => {
  log.debug({ typeCheck: typeof t.childrens, children: t.childrens });
  let outputChildren: any[] = [];
  if (typeof t.childrens !== undefined) {
    t.childrens.forEach((c1: any) => {
      if (typeof c1.childrens !== undefined) {
        outputChildren.concat(filteredChildren(c1));
        outputChildren.push(c1);
      } else {
        outputChildren.push(c1);
      }
    });
    return outputChildren;
  } else {
    return ['no child'] as any[];
  }
};

The issue I'm facing is that I'm continually getting this error either in the client or graphiql is this:

"Cannot read property 'forEach' of undefined"

I want to say that it has to do with either the forEach in filteredChildren or inside the resolver itself. I'm going through these "gymnastics" in order to get a flat array that is retrieved recursively from the underlying data. How would someone check the array to see if it's filled or not? (or in this case, if the array exists at all?)

1

1 Answers

0
votes

The condition typeof t.childrens !== undefined is always true. You should either use typeof t.childrens !== "undefined" or t.childrens !== undefined.