0

Going in circles here and hope someone can help guide me. Trying to build an array from a query to later use for a grid table. However, something I am unfamiliar with happens when I try to console.log using the array after the awaited section.

console.log(strandTable.length) comes back as 0. console.log(strandTable) comes back with some empty square brackets [], but when I expand it shows all my data. So all the data is first in a NULL section.

when I try console.log(strandTable[0]) I get an undefined.

    async function viewStrandInfo() {

      var strandTable = []

      let cableObjectId = view.popup.selectedFeature.attributes.OBJECTID

      let bufferQuery = {
        outFields: ["OBJECTID", "BUFFERTUBENUMBER", "BUFFERTUBECOLOR", "BUFFERTUBENAME"],
        relationshipId: fibrecableLayer.relationships[0].id,
        objectIds: cableObjectId
      }

      let buffers = await fibrecableLayer.queryRelatedFeatures(bufferQuery)

      await buffers[cableObjectId].features.forEach((buffer) => {
        let bufferObjectId = buffer.attributes.OBJECTID

        let strandQuery = {
          outFields: ["OBJECTID", "FIBERNUMBER", "FIBERCOLOR", "ADMINLABEL", "STATUS", "OWNER", "ADMINISTRATOR", "HOT_CIRCUIT_ID", "COMMENTS", "RESERVEDFOR"],
          relationshipId: buffTable.relationships[0].id,
          objectIds: bufferObjectId
        }

        buffTable.queryRelatedFeatures(strandQuery).then((result) => {
          result[bufferObjectId].features.forEach((strand) => {
            strandTable[strand.attributes.FIBERNUMBER] = {
              strand: strand.attributes.FIBERNUMBER,
              buffer: buffer.attributes.BUFFERTUBENUMBER,
              adminlabel: strand.attributes.ADMINLABEL,
              status: strand.attributes.STATUS,
              owner: strand.attributes.OWNER,
              admin: strand.attributes.ADMINISTRATOR,
              cctid: strand.attributes.HOT_CIRCUIT_ID,
              comments: strand.attributes.COMMENTS,
              reservedfor: strand.attributes.RESERVEDFOR    
            }
          })
        })
      })

      console.log(strandTable.length)
      console.log(strandTable)

    };
Luka B.
  • 1
  • 1
  • `await buffers[cableObjectId].features.forEach(...` this will resolve immediately since `forEach` **always** returns `undefined`. Have a look at [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) – Đinh Carabus Jan 29 '22 at 21:54
  • Don't use `forEach`, but `for..of`. Don't use `then`, but `await`. You'll have less problems. – trincot Jan 29 '22 at 21:58

0 Answers0