Escribí una función simple de búsqueda en Vanilla JS para obtener los datos de la API. Quiero enviar estos datos a mi base de datos mongodb usando el nodo js. Sé que este código es un caos, pero mi amigo está trabajando en el backend y yo estoy trabajando en el frontend y estamos tratando de fusionar ese código. No sé qué hacer con este argumento 'req' en la función de creación. Gracias por ayudar.
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;req en esta api se usa para obtener datos del cuerpo o parámetros de consulta desde el front-end, por ejemplo, está enviando algunos datos en la solicitud posterior al backend, en backend api req.body contiene el objeto que envió desde el front-end, por ejemplo, desea cree un nuevo usuario y está enviando el nombre de usuario y la contraseña a la API de backend, este requisito contiene un objeto
{ name:'Some Name', password:'somePassword' }