简体   繁体   中英

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;

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'
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM