Harish
Harish

Reputation: 123

TypeError: Cannot read property 'session' of null

Can anyone tell me what is the error in my code. I couldn't find out my error___________________________________________________________________________

router.get('/:filename', (req,res) => {
    const img = req.params.filename; // Filename
    gfs.collection('wdress').findOne({filename: img}, (req,file) => {
        if(req.session.name==null) {
            req.session.name = [{
                brand: img
            }]
        } else {
            req.session.name.push({
                brand: img
            });
        }
    });
});

Error

 TypeError: Cannot read property 'session' of null
  at gfs.collection.findOne (M:\FinalProject\Commerce\routes\index.js:186:8)
 at result 
 (M:\FinalProject\Commerce\node_modules\mongodb\lib\utils.js:414:17)

app.js

app.use(function(req, res, next) {
 res.locals.items = req.session;
 next();
});

Upvotes: 0

Views: 1609

Answers (1)

coockoo
coockoo

Reputation: 2392

I guess this callback

gfs.collection('wdress').findOne({filename: img}, (req,file) => {

should accept the first parameter as error.

gfs.collection('wdress').findOne({filename: img}, (error,file) => {

And you've got error = null in the callback and req is overlapping with another req from the higher scope (req,res). Looks like this was a copy/paste typo.

Upvotes: 1

Related Questions