Home > Net >  Express ( node.js ) - TypeError: Cannot read properties of null
Express ( node.js ) - TypeError: Cannot read properties of null

Time:10-04

I use Express ( node.js ) and MongoDB. When I try to view or update user profile I get an error

middleware token check worked
    getProfileFields:::::::::::::>>>>e:  TypeError: Cannot read properties of null (reading 'minAge')
    at module.getProfileFields (C:\localhost\website\app\controllers\api\profile.js:34:22)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

it seems problem with this part of code

module.getProfileFields = async (req, res) => {
        var successMessage = { status: 'success', message:"", data:{}};
        var failedMessage = { status: 'fail', message:"", data:{}};
        try {
            var profileFields = await model.ProfileFields.find({isDeleted:false},{slug:1, options:1,type: 1});
            var obj = {
                ...
                educationLevel: [],
                hobbies: [],
                whatYouSay: [],
                minAge: settings.minAge ? settings.minAge : 0,
                maxAge: settings.maxAge ? settings.maxAge : 0,
                religion: []                
            };
            

            }

            successMessage.message = "Successfully loaded profile reference";
            successMessage.data = obj;
            res.send(successMessage);
        } catch (e) {
            console.log('getProfileFields:::::::::::::>>>>e: ',e);
            failedMessage.message = "Something went wrong";
            res.send(failedMessage);
        }
    }
    
    
    

How to solve this problem?

CodePudding user response:

Settings are empty, maybe they are not exported correctly. Check your settings file.

CodePudding user response:

I think you're not using the find API call for MongoDB properly, find usually takes up a filter object and an object of properties as a second argument. Check the syntax required for find(){} function and probably you'll get through with it.

Hope it helps. Happy coding!!

  • Related