Получаю эту ошибку, и я прочитал так много постов безрезультатно.
Любая помощь будет принята с благодарностью, так как я пытаюсь понять это так долго!
(node:2225) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2225) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Строка, на которую указывает ошибка.
at /Users/[redacted/Desktop/prod/track-server/src/routes/trackRoutes.js:11:53
Код, о котором идет речь.
const express = require('express');
const mongoose = require('mongoose');
const requireAuth = require('../middlewares/requireAuth');
const Track = mongoose.model('Track');
const router = express.Router();
router.use(requireAuth);
router.get('/tracks', async (req, res) => {
const tracks = await Track.find({ userId: req.user._id });
try {
res.send(tracks);
} catch (err) {
res.status(422).send({ error: err.message });
}
});
router.post('/tracks', async (req, res) => {
const { name, locations } = req.body;
if (!name || !locations) {
return res.status(422).send({ error: 'You must provide a name and locations' });
}
try {
const track = new Track({ name, locations, userId: req.user._id });
await track.save();
res.send(track);
} catch (err) {
res.status(422).send({ error: err.message });
}
});
module.exports = router;