0

I am having an issue with data duplicating on my table For example, I have this data

let data = [{ name: Tope},{ name: Wale},{ name: Tope}];

  models.sequelize.Promise.map(data, pValue => {
    return models.User.findOrCreate({
      where: {
        name: pValue.name,
      },
      defaults: {
        name: pValue.name,
        organization_id: req.user.org_id
      }
    }).then(result => {
      let resultData = result[0], 
          created = result[1]; // i am getting true 3x
        console.log(created);
        console.log(resultData.get());
    })
  });

I have used this link, but the flow did not work for me Node + Sequelize: How to check if item exists before adding? (async confusion)

  • Best method to battle data duplication is by adding a UNIQUE Constraint within PostgreSQL's table. Read about that here https://www.postgresql.org/docs/9.4/static/ddl-constraints.html – Raymond Nijland Apr 28 '18 at 12:08
  • Thanks for the comment, but the major problem is that anytime i do db.Model.findOrCreate inside an async map its not finding the data and also the created params as the second param is returning true instead of false if the record exists. – Adeyemi Kayode Apr 28 '18 at 13:50

0 Answers0