Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'headers' of undefined on Node.Js

I'm trying to do a simple form submission with Node.js and Express, it's my first time working with this technologies and to be honest I'm stuck.

I got 3 javascript scripts and 1 html page. One of the script is Index.js, where I'm running the server with node.js and express, other is to verify the input on the html and an Ajax call with XMLHttpRequest object if all the data is correct, and the last one is the one who handle the HTTP request and saves the input data on a .json file.

At this moment I'm yet not saving the input data in the .json file.

The problem I'm facing is I'm getting the next error: TypeError: Cannot read property 'headers' of undefined

I don't know how to solve it but I know the error is in the script which manage the HTTP request to save it on the json file.

This is the code which trows error:


const https = require('https');
const fs = require('fs');
const parse = require('querystring');


class SignUp {

  collect(req, res) {
    collectReqData(req, res => {
      console.log(res);
    });

    function collectReqData(request, callback) {
      const contentType = res.headers['content-type'];
      const DATA_FORM = 'multipart/form-data';

      if(contentType === DATA_FORM) {

        let body = '';

        request.on('data', chunk => {
          body += chunk.toString();
        });

        request.on('end', () => {
          callback(parse(body));
        });

      }
      else{
        callback(null);
      }
    }

  }

}

var newRequest = new SignUp();

newRequest.collect();

module.exports = SignUp;

Next the Ajax call on the verification script:

    let formElement = document.getElementById('myForm');
    var request = new XMLHttpRequest();
    request.open('POST', './signUp.js');
    request.send(new FormData(formElement));

And then Index.js file:

'use strict';

const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname,'index.html')); 
});

app.get('/submit.js', (req, res) => {
  let k = fs.readFileSync('./submit.js', 'utf8');
  res.send(k);
});

app.post('/signUp.js', (req, res) => {
  let signUp = require('./signUp');
  res.sendStatus(200);
});

app.listen(3000, () => {
  console.log('server started');
});

Please if someone could help me and explain what am I doing wrong I'll be really thankful, and I'll also appreciate any suggestions on my code because I'm new which this technology and it will be really helpful.

like image 813
F2BEAR Avatar asked Mar 15 '26 18:03

F2BEAR


1 Answers

You've defined your collect function as collect(req, res) { (a function taking two arguments) but at the bottom of your file you call newRequest.collect(); (passing zero arguments).

The collect function attempts to access req.headers, but you didn't pass a request, so it's undefined. Hence the error: "Cannot read property 'headers' of undefined".

It looks like you're trying to call the collect function with the request and response from your app.post('/signUp.js' route. In that case, you simply need to call collect there instead of in the file, so that you can pass the request and response:

app.post('/signUp.js', (req, res) => {
  let signUp = require('./signUp');
  signUp.collect(req, res);
  res.sendStatus(200);
});

Ensure you remove the faulty line from signUp.js and export a usable class instance, not the class:

//bottom of signUp.js
module.exports = new SignUp();
like image 66
Klaycon Avatar answered Mar 17 '26 15:03

Klaycon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!