Mongo access using fetch javascript

0

Hello, I'm developing a simple system with nodejs and javascript. The system consists of the registration of a driver, a passenger and a race containing a driver and a passenger. I am using mongodb as a database.

I have already created the API on node to persist in the database and do the services, and I am using the fetch library to consume in javascript and put it on the page.

The question is, when registering a race with both participating: I used input radio to select one of each, get their id's and register them in the bank and using their ids could get their data. I am having trouble accessing two get's methods together to retrieve the ids and persist. I did a schematic, it registers, but it registers one and then the other, in different registers (I want the same record).

Follow the code to analyze:

addNovaCorrida = () => {
    var motorista = document.getElementsByName('motorista')
    var passageiro = document.getElementsByName('passageiro')


    fetch('http://localhost:3303/getMotoristas')
        .then(function(response){
            return response = response.text()
        })
        .then(function(body){
            var b = JSON.parse(body)
            for(var i in b.motoristas){
                if(motorista[i].checked){
                    console.log(b.motoristas[i]._id)
                    fetch('http://localhost:3303/addCorrida', {
                        method: 'POST',
                        headers: {
                            Accept: 'application/json',
                            'Content-Type': 'application/json',
                        },
                            body: JSON.stringify({
                            id_Motorista: b.motoristas[i]._id
                            }),
                    });

                }
            }
        }),

    fetch('http://localhost:3303/getPassageiros')
        .then(function(response){
            return response = response.text()
        })
        .then(function(body){
            var b = JSON.parse(body)
            for(var i in b.passageiros){
                if(passageiro[i].checked){
                    console.log(b.passageiros[i]._id)
                    fetch('http://localhost:3303/addCorrida', {
                        method: 'POST',
                        headers: {
                            Accept: 'application/json',
                            'Content-Type': 'application/json',
                        },
                            body: JSON.stringify({
                            id_Passageiro: b.passageiros[i]._id
                            }),
                    });

                }
            }
        })

}

I placed one fetch inside the other, there it makes an access and then it does the persistence, in the described order. I wanted to persist the simulated data so I think it would end this error.

NOTE: var driver and passenger are the radios, of the tables that I display and capture their ids.

Vlw galera, hug.

    
asked by anonymous 12.05.2018 / 17:37

0 answers