Monday, 5 June 2017

Attribute of cookie is undefined for specific function?

I have the following function on the server-side:

function sendUploadToGCS (req, res, next) {
  if (!req.file) {
    return next()
  }
console.log(req.session.userID)
const gcsname = req.file.originalname
const file = bucket.file(gcsname)
etc..
}

Route:

app.post('/upload-image', multer.single('image'), images.sendUploadToGCS, (req, res, next) => {
  let data = req.body

  if (req.file && req.file.cloudStoragePublicUrl) {
    data.imageUrl = req.file.cloudStoragePublicUrl
  }

  getModel().create(data, (err, savedData) => {
    if (err) {
      next(err)
      return
    }
    res.redirect(`${req.baseUrl}/${savedData.id}`);
  })
}
)

I am using express-session for cookies, and I am trying to add the userID to the name of the file being uploaded. But when I try to log it, it says undefined. That's weird because for all the other routes I have, req.session.userID does exist.

Even if I log just req.session, it does log it. It's only when I try to log req.session.userID or any other attribute says it's undefined, and only for this function (sendUploadToGCS).

Why this might be happening?



via konyv12

No comments:

Post a Comment