B
B
Bohdan Zhorov2019-07-02 10:43:32
JavaScript
Bohdan Zhorov, 2019-07-02 10:43:32

Why is the function not executed and it is not possible to get data from the database?

Good afternoon! There was a problem with requests.
There is a function on the client side - it works fine:

fetchMeetups({commit, state}, options = {}) {
            commit('setItems', {resource: 'meetups', items: []}, {root: true})

            const url = applyFilters('api/v1/meetups', options.filter)
            debugger
            return axios.get(url)
                .then(res =>{
                    const meetups = res.data
                    commit('setItems', {resource: 'meetups', items: meetups}, {root: true})
                    return state.items
                })
                .catch(err => console.log(err))
        }

url can be of 3 types:
  • /api/v1/meetups
  • /api/v1/meetups?location=shostkaua
  • /api/v1/meetups?location=shostkaua&category=dance

Routes:
const express = require('express');
const router = express.Router();

const MeetupsCtrl = require('../controllers/meetups');
const AuthCtrl = require('../controllers/auth')

router.get('', MeetupsCtrl.getMeetups);
router.get('/secret', AuthCtrl.onlyAuthUser, MeetupsCtrl.getSecret);
router.get('/:id', MeetupsCtrl.getMeetupById);
// Post Route
router.post('', AuthCtrl.onlyAuthUser, MeetupsCtrl.createMeetup);

router.post('/:id/join', AuthCtrl.onlyAuthUser, MeetupsCtrl.joinMeetup);
router.post('/:id/leave', AuthCtrl.onlyAuthUser, MeetupsCtrl.leaveMeetup);

router.patch('/:id', AuthCtrl.onlyAuthUser, MeetupsCtrl.updateMeetup)

module.exports = router;

And the function that is called on the server:
exports.getMeetups = function(req, res) {
  const {category, location} = req.query;
  
  const findQuery = location ? Meetup.find({ processedLocation: { $regex: '.*' + location + '.*' } })
                             : Meetup.find({})
  findQuery
        .populate('category')
        .populate('joinedPeople')
        .limit(6)
        .sort({'createdAt': -1})
        .exec((errors, meetups) => {
    if (errors) {
      return res.status(422).send({errors});
    }

    if (category) {
      meetups = meetups.filter(meetup => {
        return meetup.category.name === category
      })
    }

    return res.json(meetups);
  });
}

The problem is that when I send the first 2 requests - everything is OK, and when the 3rd one - it gives a 404 status and when debugging it does not even enter this function, it does not cling to breaks. What could be the snag?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question