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]