Search code examples
javascriptjsonxmlhttprequestfetchelement

Problems with fetch and XMLHttpRequest


The first thing is that I'm a noob and I know it... Maybe you think this is a repeated question but I read a lot of posts about the same question but no one helped.

I'm trying to develop a web application about working orders, clients, providers, etc... I have a Rest API with the proper routes connected to a database (mysql), and the logic with the CRUM methods. I'm still working on the server part and until some days ago everything went fine, since I tested this first with Postman and then with some simple tests and everything is working well.

The thing is I'm trying to develop the logic (the real one) of my application to access to some individual elements of the json object (the API returns an array, but that's not the problem). I need to check and generate the serial number of the working orders with this format 'number/year'. I tried with both fetch() and XMLHttpRequest to access the data and nothing works.... I can't access to the elements of the array because I always have something wrong.

If I try this inside if my tests using fetch() it works but if I try this inside my numeroOT() method I can't, I don't know what else to do so I need help please... I'm going crazy with this thing!!

This is the code that works IN MY TEST:

describe('TEST logica.js', function () {

  it('Genero el Nº de Orden', async () => {

    var numeroDeOT = laLogica.numeroOT(); //this is the method of my logic I'm testing and it's suposed to do the thing


      //This is the the only code which I could acceed to the json. But this has to go inside the numeroOT() method but only works in the test
      //-------------------------------------------

      var response = await fetch('http://localhost:3000/ots');
      var orden = await response.json();
      var laOrden = orden[orden.length-1]; //the last element/json object of the array
      var elNumero = laOrden.numero_ot; //the element I want to check and generate
      console.log(elNumero);
      //The code to check this element and generate the new one goes down here but that's clear



      //---------------------------------------------

    console.log(numeroDeOT);
    expect(numeroDeOT).to.be.a('String'); //this is to check what my method numeroOT() returns. Usually 'undefined'

  }) //it
}); //describe

Solution

  • I realized that the two ways I was trying in my code there weren't possible to use them with node (since I'm using node), so I tried this code into my method and it works perfectly and I finally can access to my array of JSON objects!

    var options = {
            host : 'localhost',
            port : 3000,
            path : '/ots', // the rest of the url with parameters if needed
            method : 'GET' // do GET
        };
    
        var request = http.request(options, function (res) {
    
            console.log("request received");
            var data = '';
            res.on('data', function (chunk) {
                data += chunk;
    
            });
            res.on('end', function () {
                console.log(data);
    
            });
        });
        request.on('error', function (e) {
            console.log(e.message);
        });
        request.end();