Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
Error using captcha and audio file in a html page

Im trying to use a captcha and send an audio file on same page. But I can't get the name of the file to send to the database. TypeError: Cannot destructure property 'filename' of 'req.file' as it is undefined.

I try to put on only one function to send the info but then the capctha doesn't work.

form.onsubmit = async e =>{
        e.preventDefault()
        form = e.target
        var data2 = new FormData(form);
        var request = new XMLHttpRequest();
        data2.append('file',this.ablob);
        request.open('post','/posts'); 
        request.send(data2);

    }

        
        
        document.getElementById('form').addEventListener('submit', e => {
        e.preventDefault();

        const captcha = document.querySelector('#g-recaptcha-response').value;



        return fetch('/posts', {
          method: 'POST',
          headers: {'Content-type': 'application/json'},
          body: JSON.stringify({captcha}),
        })
          .then(res => res.json())
          .then(data => {
            console.log(data);
            alert(data.msg);
            if (data.success) return location.reload();
          });
      });

The back-end code:

routes.post("/posts", multer(multerConfig).single('file') , async(req, res) => {

if (!req.body.captcha)
    return res.json({ success: false, msg: 'Please select captcha' });

    // Secret key
    const secretKey = '6Le1BJ4fAAAAADxI5gIDcnZcMEk5YpXNslAdos4g';

    // Verify URL
    const query = stringify({
        secret: secretKey,
        response: req.body.captcha,
        remoteip: req.connection.remoteAddress
    });
    const verifyURL = `https://google.com/recaptcha/api/siteverify?${query}`;


    // Make a request to verifyURL
    const body = await fetch(verifyURL).then(res => res.json());

    // If not successful
    if (body.success !== undefined && !body.success)
        return res.json({ success: false, msg: 'Failed captcha verification' });

    // If successful
    const {filename:name, size, key, location: url = '' } = req.file

    const post = await Post.create({
        name,
        size,
        key,
        url,
        emotion: req.body.emotion,
        age: req.body.age,
        gender: req.body.gender,
        IP: req.ip,
        description: req.body.others
    })


    return res.json({ success: true, msg: 'Captcha passed' });
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda