Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - Path and split property give an error when uploading an image to the HEROKU server

I have problems with the path and split properties when uploading images, I think that these javaScript functions are not allowed with HEROKU, I would like to know if the same problem has happened to them as well ... or if they detail any error in the code or have knowledge the reason for the error.

HEROKU Server Errors

2021-02-04T01:03:23.505925+00:00 app[web.1]: POST /api/uploadPhotoProfile 500 3.148 ms - 148
2021-02-04T01:03:23.507503+00:00 app[web.1]: TypeError: Cannot read property 'path' of undefined
2021-02-04T01:03:23.507504+00:00 app[web.1]: at uploadPhotoProfile2 (/app/controllers/user.js:349:43)
2021-02-04T01:03:23.507505+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-02-04T01:03:23.507506+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)



2021-02-04T01:03:23.505925+00:00 app[web.1]: POST /api/uploadPhotoProfile 500 3.148 ms - 148
2021-02-04T01:03:23.507503+00:00 app[web.1]: TypeError: Cannot read property 'split' of undefined
2021-02-04T01:03:23.507504+00:00 app[web.1]: at uploadPhotoProfile2 (/app/controllers/user.js:376:33)
2021-02-04T01:03:23.507505+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-02-04T01:03:23.507506+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)

When I try to test the same function in my local if it uploads the image without problem, so it must be a HEROKU issue

Running it on the server

enter image description here enter image description here

User.Controller

User.Controller

uploadPhotoProfile: function(req,res){
    //configurar el modulo mutiparty (subida de fichero)

    //recoger el fichero de la peticion
    var photoProfile = 'Avatar no subido...';
    

    if(!req.files){

        return res.status(404).send({
            status:'error',
            message: photoProfile
        });
    }
   // conseguir el nombre y la extension del archivo
   var file_path= req.files.photoProfile.path;
   var file_split = file_path.split('\');
     
    //nombre del archivo
   var file_name= file_split[2];
   
   //Extension del archivo
   var ext_split = file_name.split('.');
   var file_ext = ext_split[1];

   //comprobar extension(solo imagenes)
    if (file_ext != 'png' && file_ext !='jpg' && file_ext !='jpeg' && 
    file_ext !='gif' && file_ext !='JPG' && file_ext !='JPEG'&& file_ext !='PNG'){
        fs.unlink(file_path, () =>{
            
            return res.status(200).send({
                status:'error',
                message:'La Extension del Archivo no es valido',
                file: file_ext
                });
        });

    }else{
   //sacar el id del usuario identificado
        var userId= req.user.sub;
   //buscar y actualizar documentos de la bd
   User.findOneAndUpdate({ _id: userId}, {photoProfile : file_name}, {new:true}, (err, userUpdated)=>{
        
    if(err || !userUpdated){

        //devolver respuesta 
        return res.status(500).send({
            status:'error',
            message:'Error al guardar el usuario',
                });
            }
        return res.status(200).send({
            status:'succes',
            user : userUpdated
            });
            
        });
  
    }
},
question from:https://stackoverflow.com/questions/66051132/path-and-split-property-give-an-error-when-uploading-an-image-to-the-heroku-serv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...