GraphQL解析器返回错误“无法读取每个未定义”

时间:2019-04-02 19:46:16

标签: node.js graphql

我有一个graphql端点,我正在对该端点运行查询,并且正在构建我的解析器,该解析器在返回客户端之前先对数据进行按摩。我的查询是这样:

   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
     }
   }

我的解析器当前看起来像这样:

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[];
  }
};

我面临的问题是我在客户端或graphiql中不断收到此错误:

  

“无法读取未定义的属性'forEach'”

我想说这与forEach中的filteredChildren或解析程序本身内部有关。我正在研究这些“体操”,以便获得从基础数据递归检索的平面数组。有人将如何检查数组以查看其是否已满? (或者在这种情况下,如果数组根本存在?)

1 个答案:

答案 0 :(得分:0)

条件typeof t.childrens !== undefined始终为true。您应该使用typeof t.childrens !== "undefined"t.childrens !== undefined