skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. The issue you’re encountering likely stems from how you’re importing and using the initFoodModel function in your wishList.js and listFood.js files.

    In wishList.js, you are using initFoodModel like this:

    const Food = await initFoodModel()
    

    And in listFood.js, you are using it like this:

    const schema = await initFoodModel();
    

    The error you’re experiencing might be due to how the initFoodModel function is defined in your actual FoodModel file. If you’ve defined it using module.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:

    1. Check the FoodModel file: Ensure that the initFoodModel function is correctly defined and exported in your FoodModel file (FoodModel.js). It should look something like this:
    // FoodModel.js
    const initFoodModel = async () => {
      // Your code to initialize the Food model
    };
    
    module.exports = { initFoodModel };
    
    1. Double-check the import paths: Verify that the import paths for initFoodModel are accurate in both wishList.js and listFood.js. Make sure the paths match the actual file structure of your project.

    2. Ensure that you’re importing initFoodModel consistently: Make sure you’re using the same capitalization and naming conventions for importing initFoodModel. JavaScript is case-sensitive, so initFoodModel and initfoodModel are considered different functions.

    3. Debugging: If the issue persists, add some console.log statements to your code to help you debug. For example, in both wishList.js and listFood.js, print out messages to check if the initFoodModel 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search