file.mv is not working on Heroku

0

I'm doing an API with node.js for file transfer to a CDN however when running the function:

 file.mv(name, (error) => {

It returns the following error:

{
    "message": "Falha ao processar sua requisição",
    "data": {
        "errno": -2,
        "code": "ENOENT",
        "syscall": "open",
        "path": "/app/src/controllers/../../cache/1d3a0c5b95d942e4ca02ea3d652e91b6.xml"
    }
}

And this error only happens after I send it to my hosting, my complete method code is this:

exports.post = async(req, res, next) => {
    try{
        let folder = path.resolve(__dirname);

        let contract = await contractrepository.getByUser(req.params.id);
        let files = await repository.getByUser(req.params.id);
        let user = await userrepository.getById(req.params.id);

        if(!req.files){
            res.status(422).send({
                message: 'É necessário enviar um arquivo'
            });
            return;
        }

        //Verificando quantidade de XML
        if(files.length >= contract.plan.qtdeXML){
            res.status(400).send({
                message: 'Você atingiu o máximo de envio de XML do seu plano'
            });
            return;
        }

        let name = req.files.file.name;

        let ext = req.files.file.name.split(".")[1];
        let file = req.files.file;
        name = folder + "/../../cache/" + md5(Date.now()) + "." + ext;

        file.mv(name, (error) => {

            if(error){
                res.status(500).send({
                    message: 'Falha ao processar sua requisição',
                    data: error
                });
            }

            let request = require('request');

            let formData = {
                folder: 'xml',
                file: fs.createReadStream(name)
            };

            //Verificando os parametros
            let query = req.query;
            let queryParam = Object.getOwnPropertyNames(query);
            let qString = "?";

            if(queryParam.length > 0){
                for(let i = 0; i  {
                let response = JSON.parse(body);

                await repository.post({
                    date: Date.now(),
                    xml: "https://cdn-notamais.herokuapp.com" + response.url,
                    user: user._id
                });

                fs.unlink(name, (err) => {
                    if (err) throw err;
                    console.log(name+' was deleted');
                });

                res.status(201).send({
                    message: 'Arquivo enviado com sucesso',
                    path: "https://cdn-notamais.herokuapp.com" + response.url
                });

            });

        });
    }catch(e){
        res.status(500).send({
            message: 'Falha ao processar sua requisição',
            data: e
        });
    }

}

Finally I found the problem, as I was using a folder called cache and it was in my gitignore this was not sent to my heroku so soon at the moment of transferring the file to that folder it gave error because it did not exist on my server , so I had to manually enter it through the heroku console and create the folder.

    
asked by anonymous 11.09.2018 / 20:13

0 answers