0

Using Express in server side and Axios to send res calls. While trying to save data into MySql database, I am getting below error in console: Using sequelize.js for model/schema creation.

Error:

(node:23364) UnhandledPromiseRejectionWarning: TypeError: this.build(...).save is not a function
    at Function.create (C:\Main\Work\devchoice\node_modules\sequelize\lib\model.js:2228:8)
    at CsvParser.<anonymous> (C:\Main\Work\devchoice\src\server.js:289:60)
    at CsvParser.emit (events.js:315:20)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:23364) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch()

server.js

const manageNomineesSchema = require('./server/models/managenominees');
const ManageNomineesModel  = manageNomineesSchema(sequelize, DataTypes);

    app.post('/service/managenominees', upload.single('file'), async (req, res, next) => {
      try {
        if(req.file){
          let filePath = req.file.path;
          fs.createReadStream(filePath)
              .pipe(csv())
              .on('data', (data) => results.push(data))
              .on('end', async () => {
                console.log(results);
                let manageNominees = await ManageNomineesModel.create(results);
                //res.status(200).send(manageNominees);
                res.status(200).json({ message: "Nominees inserted successfully !"});
              });
          }
    
      } catch (e) {
        res.status(500).json({ fail: e.message });
      }
    });

enter image description here

enter image description here

soccerway
  • 10,371
  • 19
  • 67
  • 132

1 Answers1

1

.create takes an object as an argument to create 1 instance, however, you are passing an array of objects.

If you would like to insert everything in the array, use .bulkCreate.

ref: https://sequelize.org/master/class/lib/model.js~Model.html#static-method-bulkCreate

Emma
  • 8,518
  • 1
  • 18
  • 35
  • Thank you very much Emma, it worked out well, upvoted and accepted! – soccerway Aug 06 '21 at 05:43
  • `bulkCreate` can be also used to update. https://stackoverflow.com/a/54900639/2956135 – Emma Aug 06 '21 at 17:00
  • I have tried the same way, but no luck yet, it’s inserting the same records again. – soccerway Aug 06 '21 at 21:01
  • Have you added PK value to the object in array and use `updateOnDuplicate`? – Emma Aug 06 '21 at 21:05
  • I have tried as per the example:’ await ManageNomineesModel.bulkCreate(allNominees,{ updateOnDuplicate: ["name"] })’ – soccerway Aug 06 '21 at 21:56
  • I have added the table/ data image for reference. I understood ``updateOnDuplicate`` is to add which column that need to be updated ? – soccerway Aug 06 '21 at 22:06
  • `allNominees` should have `id` in object to have "update" instead of "create". like this `[{ id: 1, name: 'athbest', email: 'athin.best@test1.com' }, ...` The key is that you need to have PK in the object for updating. – Emma Aug 06 '21 at 22:41
  • If you are still having troubles, please open a new question. It is diverged from the original question. – Emma Aug 06 '21 at 22:44
  • Thank you Emma, I have opened a new question. https://stackoverflow.com/questions/68679986/how-can-i-bulk-update-records-using-sequelize-js-and-ignore-certain-columns – soccerway Aug 06 '21 at 22:56