I’m using Nextjs 13 to create a full-stack app and use Sequelize to handle the MySQL
database.
I have api
folder to handle my admin form.
in src/pages/api/test.js
when I call my model It works correctly and without error and all of my table has been created:
src/pages/api/test.js
code:
import { Contact } from "../../../models/models";
export default async function handler(req, res) {
const contact = await Contact.findByPk(1);
return res.status(200).json({ message: "Hello World!", contact });
}
but when I use my contact model in my src/app/(admin)/admin/contact/page.js
I got an error Module not found: Can't resolve 'pg-hstore'
error screen shot:
src/app/(admin)/admin/contact/page.js
code:
import { AdminCard } from "../../../../../components/admin/UI/admin-components";
import AdminContactForm from "../../../../../components/admin/pages/contact-form";
import { Contact } from "../../../../../models/models";
async function AdminContact(props) {
// const data = await Contact.findByPk(1);
return (
<AdminCard title="Contact">
{/* <AdminContactForm data={data} /> */}
</AdminCard>
);
}
export default AdminContact;
model contact.js
code:
import Sequelize from "sequelize";
import sequelize from "./database";
const Contact = sequelize.define("contact", {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
email: {
type: Sequelize.STRING,
allowNull: true,
},
address: {
type: Sequelize.STRING,
allowNull: true,
},
phone: {
type: Sequelize.STRING,
allowNull: true,
},
instagram: {
type: Sequelize.STRING,
allowNull: true,
},
facebook: {
type: Sequelize.STRING,
allowNull: true,
},
linkedin: {
type: Sequelize.STRING,
allowNull: true,
},
whatsapp: {
type: Sequelize.STRING,
allowNull: true,
},
});
sequelize
.sync()
.then((result) => {
// console.log("result1 => ", result);
})
.catch((err) => {});
export default Contact;
I’m using Mysql, not Postgres.
2
Answers
It's solved by running
npm install pg-hstore
.I’m currently facing difficulties with connecting a database using Sequelize in Next.js 13. Could you please share your project?