Hey guys I am building a fullstack website to to learn Express , Sequelize and REST api
I have this model for a post request “
module.exports = (sequelize, DataTypes) => {
const PaymentMethods = sequelize.define("payment_methods", {
email: {
type: DataTypes.STRING,
allowNull: false,
},
cardnumber: {
type: DataTypes.STRING,
allowNull: false,
},
expirydate: {
type: DataTypes.DATE,
allowNull: false,
},
CVC: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
timestamps: false
});
return PaymentMethods;
};
Then we use it in this file
const express = require('express');
const router = express.Router();
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const db = require('../models');
router.post('/payment/updatepaymentmethod', async (req, res) => {
try {
const { email, cardNumber, expDate, cvc } = req.body;
// Create a new payment method record in the database
const newPaymentMethod = await db.PaymentMethods.create({
email: email,
cardnumber: cardNumber,
expirydate: expDate,
CVC: cvc
});
// Return success response
res.status(200).json({
success: true,
message: 'Payment method updated successfully',
paymentMethod: newPaymentMethod
});
} catch (error) {
console.error('Error during payment method update:', error);
// Log the error object for debugging
console.log(error);
res.status(500).json({
success: false,
message: 'Payment method update failed',
error: error.message
});
}
});
module.exports = router;
I get this error
"error": "Cannot read properties of undefined (reading 'create')"
If I change the method to update it still does so.
And the weirdest thing is that I have another request on a different model but with exactly the same structure and it works perfectly fine.
I tried several things to fix it but none of them worked or I ran into stuff I don’t really know, like changing the index.js file properties From what I understood the problem is the model importing into my file.
It should allow me to push some data to my database table payment_methods