Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Node JS, vanilla JS

i wrote a simple function fetch in vanilla JS to get te data from the api. I want to send this data to my database mongodb using node js. I know this code is a chaos, but my friend is working on the backend and i am working on the frontend and we are trying to merge that code. I don't know what to do with this 'req' argument in create function. Thank you for help.


const Match = require('../db/models/match');

class MatchController {
    async showMatches(req, res) {        
        const matches = await Match.find();
        res.status(200).json(matches); //parsuje dane na JSON 
        console.log('show');
    }

    async create(req, res) { 

        const match = getDataForSheduleCJS(nameLeague).then(resp => {
            const matchObject = new Match({
                leagueName: resp.competition.name,
                date: resp.utcDate.slice(0, 10),
                awayTeam: resp.awayTeam.name,
                homeTeam: resp.homeTeam.name,
                scoreHomeTeam: resp.score.fullTime.homeTeam,
                scoreAwayTeam: resp.score.fullTime.awayTeam,             
            });
            return matchObject;
        });
     

        // const match = new Match({
        //     leagueName: 'Bundes'
        // });

        try {
            console.log(match);
            await match.save();
            //res.status(201).json(match); 
        } catch (e) {
            console.log('error');
            res.status(422).json({
                errors: e.errors
            }); 
        }
    }
}

module.exports = new MatchController();
    const getDataForSheduleCJS = () => {
            fetch(`https://api.football-data.org/v2/competitions/PL/matches`, {
                headers: {
                  'Content-Type': 'application/json',
                  'X-Auth-Token': personalToken
            }}).then(resp => resp.json())
            // .then(data => data)
            .catch((error) => {
                alert("Wystąpił problem z danymi")
                console.error('Error:', error);
            });
        }


const express = require('express');
const router = new express.Router(); //zmieniamy nazwy z app.get na router.get
const MatchController = require('../controllers/match-controller');

router.get('/matches', MatchController.showMatches);
router.post('/matches', MatchController.create);

module.exports = router;

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

req in this api is used to get body data or query params from front-end for example you are sending some data in post request to backend, in backend api req.body contains object u sent from front-end , for example you want to create a new user and you are sending user name and password to backend api this req contains object

{
  name:'Some Name',
  password:'somePassword'
} 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda