Shannon Rose
Shannon Rose

Reputation: 29

PERN stack: Iterating through array of data, my call to my database is not going through for every data record

I have been trying like crazy to find a solution to my problem, but nothing seems to work and I don't know where I am going wrong. I am creating an app using the PERN stack, and I have an array of data with a length of 24. I iterate through my data array with following snippet of code (this is after trying to find solutions but the result is always the same):

const createEntry = async function (data) {
  let whatever = await Promise.all(
    data.map(async (item) => {
      try {
        console.log(`${item.name}`);
        await Entry.post("/", item); //call to database
      } catch (err) {
        console.log(err);
      }
    })
  );
  whatever.then(console.log("I hate my life."));
};

I know the entire data array is being iterated through because of the console.logs, but the call to Entry.post() is only happening like maybe six times, and I am not getting all of my data entered into my database. My express app.post code looks like this:

app.post("/url", async (req, res) => {
  try {
    const results = await db.query(
      "INSERT INTO database (id, name) values ($1, $2)",
      [
        req.body.id,
        req.body.name,       
      ]
    );
    res.send({
      status: "success",
      results: results.rows.length,
      data: {
        entry: results.rows[0],
      },
    });
  } catch (err) {
    console.log(`${err.detail} for ${req.body.name}`);
  }
});

Upvotes: 2

Views: 33

Answers (1)

Shannon Rose
Shannon Rose

Reputation: 29

So, I resolved this on my own and found a working solution.

My createEntry code from my question:

const createEntry = async function (data) {
  let whatever = await Promise.all(
    data.map(async (item) => {
      try {
        console.log(`${item.name}`);
        await Entry.post("/", item); //call to database
      } catch (err) {
        console.log(err);
      }
    })
  );
  whatever.then(console.log("I hate my life."));
};

now looks like this:

const createEntry = async function (data) {
  try {
    let result = await CreateDB.post("/", data);
    return result;
  } catch (err) {
    console.log(err);
  }
};

And my app.post code

app.post("/url", async (req, res) => {
  try {
    const results = await db.query(
      "INSERT INTO database (id, name) values ($1, $2)",
      [
        req.body.id,
        req.body.name,       
      ]
    );
    res.send({
      status: "success",
      results: results.rows.length,
      data: {
        entry: results.rows[0],
      },
    });
  } catch (err) {
    console.log(`${err.detail} for ${req.body.name}`);
  }
});

Now looks like this:

app.post("/url", async (req, res) => {
  try {
    const results = await db.query(
      "INSERT INTO database (id, name) values ($1, $2)",
      [
        req.body.id,
        req.body.name,       
      ]
    );
    res.send(res.rows[0]);
  } catch (err) {
    console.log(`${err.detail} for ${req.body.name}`);
  }
});

And my call to my createEntry is:

let temp = {obj: some object};
createEntry(temp).then((newEntry) => {
                  dbArray.push(newEntry.data);
                  manipulateData(newEntry.data);
                });

And with this I am now able to create a database entry, retrieve the database object and do work with it and it works for any size array which makes me really happy. So hopefully, if anyone has a similar problem, this can help.

Upvotes: 0

Related Questions