Sequelize insert multiple rows into MySQL table(NodeJS Problem)

Viewed 68

I'm a beginer at NodeJS, I am Learning Sequelize framework and trying interactive with database. everything is okay until i'm writing insert into table function by sequelize model, Here is My cdoe:

const {Sequelize, DataTypes} = require("sequelize");
const sequelize = new Sequelize("task_management","root","Th@ng123",{
host : "localhost",
dialect : "mysql",
});
//Tao 1 sequelize model
const Task = sequelize.define(
 "Task",
{
    //Columns in table
    name:{
        type: Sequelize.STRING, //string
        allowNull: false,
    },
    status: {
        type : DataTypes.STRING, //STRING
    }
}
)
//Synchronus function <=> Create a new Table in database
  const syncModel = async ()=>{
  await Task.sync({force: true});
  console.log("Task sync model complete");
  }
   // syncModel();
   const createTask = async () => {
   // c1
const newTask = Task.build({ name: "Learning NodeJS", status: "OPEN" });
await newTask.save();
// c2
const golangTask = await Task.create({
  name: "Learning Golang",
  status: "OPEN",
 });
};  
createTask();

  // Check connection
   const checkConnect = async ()=>{
   try {
    await sequelize.authenticate();
    console.log("Task check connect complete");
} catch (error) {
  console.error("Unable connect Database")
}

}

But the result is with each Create code, in database inserted 3 rows. Like this enter image description here

What happen and what i can do with this case ? Tell me please Thank you!

0 Answers
Related