I working on creating a detabase using mysql and node.js and sequelize . my model is saved on my database as i ordered but when i want send data it does not work
here is my codes
Database.js
const { Sequelize } = require("sequelize");
const sequelize = new Sequelize("node-compelet", "root", "zahra@1996", {
dialect: "mysql",
host: "localhost",
});
module.exports = sequelize;
and my Model called product.js
const sequelize = require("../util/database");
const { Sequelize, DataTypes } = require("sequelize");
const Product = sequelize.define("Product", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
title: { type: DataTypes.STRING },
price: { type: DataTypes.DOUBLE, allowNull: false },
imgUrl: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.STRING, allowNull: false },
});
module.exports = Product;
and my controller :
const Product = require("../models/product")
exports.postAddProduct = (req, res, next) => {
const title = req.body.title;
const imgUrl = req.body.imgUrl;
const price = req.body.price;
const description = req.body.description;
console.log(req.body);
Product.create({
title: title,
price: price,
imgUrl: imgUrl,
description: description,
})
.then((result) => {
console.log("result");
res.redirect("/");
})
.catch((err) => console.log(err));
};
and my app.js
const sequelize = require("./util/database");
const Product = require("../models/product")
sequelize
.sync({ force: true })
.then((result) => {
app.listen(3000);
console.log("synced secceed");
})
.catch((err) => {
console.log(err);
console.log("faild");
});
every time I tried to send data from my form to database nothing happens and just my web page continue loading . also in my controller I tried to log received data but it didn’t worked . I got no error message . it seems that my model don’t work properly .
I checked router and my functions was added properly there .