wishList.js
const { initFoodModel } = require("../../model/FoodModel");
const { initWishlistModel } = require("../../model/wishListModel");
const initWishlistModel = async () => {
try {
const sequelize = await getConnection();
const Food = await initFoodModel()
const User = await initUserModel()
const Wishlist = sequelize.define("Wishlist", wishlistModel, {
freezeTableName: true,
});
Wishlist.belongsTo(Food, { foreignKey: 'Food_id', targetKey: 'Food_id' });
Wishlist.belongsTo(User, { foreignKey: 'user_id' });
await Wishlist.sync({ alter: true });
return Wishlist;
} catch (err) {
console.log(err.message);
}
};
module.exports = { initWishlistModel };
listFood.js
const {initWishlistModel} = require("../../model/wishListModel");
const { initFoodModel } = require("../../model/FoodModel");
const getFood = async (req, res) => {
try {
const { Food_id } = req.params
const user_id = req.token.user.id;
console.log(req.token.user.id);
const schema = await initFoodModel();
const wishlist = await initWishlistModel();
let isWishlisted = await wishlist.findAll({ where: { Food_id: Food_id, user_id: user_id } })
console.log(isWishlisted);
if (isWishlisted) {
return send(res, RESPONSE.SUCCESS, "already wishlisted")
}
return send(res, RESPONSE.ERROR })
// let check = await schema.findOne({ where: { Food_id: Food_id, stat: true }, });
// if (check.length === 0) {
// return send(res, RESPONSE.ITM_NOT_FOUND);
// }
// return send(res, RESPONSE.SUCCESS, { check })
} catch (err) {
console.log(err.message);
return send(res, RESPONSE.ERROR, err.stack)
}
};
i want to check whether the given food_id is there in wishlist or not
i have already checked all the files, the initFoodModel is properly defined
this error occurs only when i try to use initFoodModel in wishList, if i use initfoodModel directly it works fine. [check commented lines]
2
Answers
i have been using unwanted circular requires, thats why i ended up with empty objects,I would suggest to remove all the unnecessary requires when you don't need the other models.
The issue you’re encountering likely stems from how you’re importing and using the
initFoodModel
function in yourwishList.js
andlistFood.js
files.In
wishList.js
, you are usinginitFoodModel
like this:And in
listFood.js
, you are using it like this:The error you’re experiencing might be due to how the
initFoodModel
function is defined in your actualFoodModel
file. If you’ve defined it usingmodule.exports
in a way that doesn’t export it properly, it could lead to the error you’re facing.Here are a few steps to help diagnose and potentially fix the issue:
FoodModel
file: Ensure that theinitFoodModel
function is correctly defined and exported in yourFoodModel
file (FoodModel.js
). It should look something like this:Double-check the import paths: Verify that the import paths for
initFoodModel
are accurate in bothwishList.js
andlistFood.js
. Make sure the paths match the actual file structure of your project.Ensure that you’re importing
initFoodModel
consistently: Make sure you’re using the same capitalization and naming conventions for importinginitFoodModel
. JavaScript is case-sensitive, soinitFoodModel
andinitfoodModel
are considered different functions.Debugging: If the issue persists, add some console.log statements to your code to help you debug. For example, in both
wishList.js
andlistFood.js
, print out messages to check if theinitFoodModel
function is being imported and called correctly.By following these steps and making sure your function is exported and imported consistently, you should be able to resolve the issue.