javascript – For loop & forEach returns undefined

I’m working on a function that requires to do comparison between index in Array of object A with indexes in Array of objects B So I request from 2 collections data I get arrays :

TPE info [
  {
    _id: new ObjectId("62826ee8e052bb423620478a"),
    sn: 'N300W150726',
    terminalId: '05000002',
    merchantId: '458742236657711',
    merchantName: 'TEST MERCHANT',
    networkType: 'wifi',
    __v: 0
  },
  {
    _id: new ObjectId("6287b9cf13f593e720de9a67"),
    sn: '123test123',
    terminalId: '123456789',
    merchantId: '9000000058800',
    merchantName: 'lassoued',
    networkType: '220509',
    __v: 0
  }
]


Log info [
  {
    _id: new ObjectId("6289723bae84079756321141"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("6289747da3c8fe63b77cb339"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628975028836a7e921f4bc88"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897556bfdcc06d3e50ade2"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897573acb25689a25fce5    Log info [
  {
    _id: new ObjectId("6289723bae84079756321141"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("6289747da3c8fe63b77cb339"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628975028836a7e921f4bc88"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897556bfdcc06d3e50ade2"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897573acb25689a25fce59"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897bdf51dbe6a5615fc068"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c5d1cc5e49a239787a9"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c8ac46ef77ab782a448"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897e1ba1a6b67a69d38495"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897fc8a283cf411b216f5b"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984389c20a805ce7b75d5"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984a5ccc646d624f14996"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  }]9"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897bdf51dbe6a5615fc068"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c5d1cc5e49a239787a9"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c8ac46ef77ab782a448"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897e1ba1a6b67a    Log info [
  {
    _id: new ObjectId("6289723bae84079756321141"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("6289747da3c8fe63b77cb339"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628975028836a7e921f4bc88"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897556bfdcc06d3e50ade2"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897573acb25689a25fce59"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897bdf51dbe6a5615fc068"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c5d1cc5e49a239787a9"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897c8ac46ef77ab782a448"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897e1ba1a6b67a69d38495"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897fc8a283cf411b216f5b"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984389c20a805ce7b75d5"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984a5ccc646d624f14996"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  }]69d38495"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("62897fc8a283cf411b216f5b"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984389c20a805ce7b75d5"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("628984a5ccc646d624f14996"),
    uniqueID: '63030824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  }]

I want to compare terminalId (TPE) with terminalId(log), so I have to loop through array of TPE get terminalId of each object index and compare it with terminalId of log

MY CODE

async affectSnTPEtoLogs() {
    let getSN = await this.tpeModel.find();
    let snIsExist = await this.logModel.find();
    // ARRAY OF OBJECT DOESN'T CONTAIN SN FIELD
    let isSnExistInTransaction = await this.logModel.find({ sn: null });
    // CHECK IF I HAVE MANY TRANSACTION WITHOUT SN
    if (isSnExistInTransaction.length > 1) {
      for (let i = 0; i <= getSN.length; i++) // returns undefined
        var x = isSnExistInTransaction.filter(
          (item) => item.terminalId === getSN[i].terminalId,
        );
    }

Even I tried with forEach I got undefined too , what’s the prblem?

Leave a Comment