'I can't figure out why I'm getting a ReferenceError: fileUploaded is not defined

Hi this is my first post! Here's the error I'm throwing when trying to submit a post on a website I'm making! This is the error and line 36 is where i call sharp(fileUploaded): C:\Users\tester\OneDrive\Desktop\project-user\application\routes\posts.js:36 sharp(fileUploaded) ^enter image description here

ReferenceError: fileUploaded is not defined at Object. (C:\Users\xxwaz\OneDrive\Desktop\csc317-termproject-dmoorhatch\application\routes\posts.js:36:7) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (C:\Users\xxwaz\OneDrive\Desktop\csc317-termproject-dmoorhatch\application\app.js:10:21) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)

var express = require('express');
var router = express.Router();
var db = require('../config/database');
const { errorPrint, successPrint } = require('../helpers/debug/debugprinters');
var sharp = require('sharp');
var multer = require('multer');
var crypto = require('crypto');
const { syncBuiltinESMExports } = require('module');
const { request } = require('http');
const { registerValidator } = require('../middleware/validation');

var storage = multer.diskStorage({
    destination: function(req, file, cb){
        cb(null, "public/images/uploads");
},
filename: function(req, file, cb){
        let fileExt = file.mimetype.split('/')[1];
        let randomName = crypto.randomBytes(22).toString("hex");
        cb(null, `${randomName}.${fileExt}`);
    }
});

var uploader = multer({storage: storage});


router.post('/createPost', postValidation, uploader.single("uploadImage"),(req, res, next) => {
    let fileUploaded = req.file.path;
    let fileAsThumbnail = `thumbnail-${req.file.filename}`;
    let destinationOfThumbnail = req.file.destination + "/" +fileAsThumbnail;
    let title = req.body.title;
    let description = req.body.description;
    let fk_userId = req.session.userId;
})


sharp(fileUploaded)
.resize(200)
.toFile(destinationOfThumbnail)
.then(() =>{
    let baseSQL = 'Insert into post (title, description, photopath, thumbnail, created, fk_userid) VALUE (?, ?, ?, ?, now(), ?);;';
    return db.execute(baseSQL, [title, description, fileUploaded, destinationOfThumbnail, fk_userId]);
})
.then(([results,fields]) =>{
    if(results && results.affectedRows){
        res.redirect('/');
    }else{
        res.redirect('/postImage');
    }
})
.catch((err) => {

})

module.exports = router;


Solution 1:[1]

do this, you needed to add your code to the callback function

var express = require('express');
var router = express.Router();
var db = require('../config/database');
const { errorPrint, successPrint } = require('../helpers/debug/debugprinters');
var sharp = require('sharp');
var multer = require('multer');
var crypto = require('crypto');
const { syncBuiltinESMExports } = require('module');
const { request } = require('http');
const { registerValidator } = require('../middleware/validation');

var storage = multer.diskStorage({
    destination: function(req, file, cb){
        cb(null, "public/images/uploads");
},
filename: function(req, file, cb){
        let fileExt = file.mimetype.split('/')[1];
        let randomName = crypto.randomBytes(22).toString("hex");
        cb(null, `${randomName}.${fileExt}`);
    }
});

var uploader = multer({storage: storage});


router.post('/createPost', postValidation, uploader.single("uploadImage"),(req, res, next) => {
    let fileUploaded = req.file.path;
    let fileAsThumbnail = `thumbnail-${req.file.filename}`;
    let destinationOfThumbnail = req.file.destination + "/" +fileAsThumbnail;
    let title = req.body.title;
    let description = req.body.description;
    let fk_userId = req.session.userId;

    // put code here
    sharp(fileUploaded)
    .resize(200)
    .toFile(destinationOfThumbnail)
    .then(() =>{
        let baseSQL = 'Insert into post (title, description, photopath, thumbnail, created, fk_userid) VALUE (?, ?, ?, ?, now(), ?);;';
        return db.execute(baseSQL, [title, description, fileUploaded, destinationOfThumbnail, fk_userId]);
    })
    .then(([results,fields]) =>{
        if(results && results.affectedRows){
            res.redirect('/');
        }else{
            res.redirect('/postImage');
        }
    })
    .catch((err) => {

    })
})



module.exports = router;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dean Van Greunen