** Я пытаюсь получить один идентификатор из списка, но он не возвращает данные, как ожидалось ... **
GET / article / 5b0be8829f734a4e580a43c5 401 3,845 мс - 99 ===> мой ответ на запрос get
my api ===>
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken');
var User = require('../models/User')
var Article = require('../models/Article');
router.get('/', function (req, res, next) {
Article.find()
.populate('user')
.exec(function (err, articles) {
if (err) {
return res.status(500).json({
title: 'An error occured getting articles',
error: err
});
}
res.status(200).json({
message: "Success",
obj: articles
});
});
});
//I'm having issue with this route below
//I'm having issue with this route below
router.get('/article/:articleId', function (req, res, next) {
// Check if the blog id is found in database
// var decoded = jwt.decode(req.query.token);
Article.findById(req.params.articleId, function (err, article) {
// if the ID is not found or invalid, return err
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
// if the article was not found anyways
if (!article) {
return res.status(500).json({
title: 'Article not found',
error: { message: 'Article was not found!' }
});
}
res.status(200).json({
message: 'successful :id',
obj: article
});
});
});
//ROAD-BLOCK => { (checking if you're authenticated(true))}
router.use('/', function (req, res, next) {
jwt.verify(req.query.token, 'secret', function (err, decoded) {
if (err) {
return res.status(401).json({
title: 'Not Authenticated',
error: err
});
}
next();
})
});
router.post('/', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
User.findById(decoded.user._id, function (err, user) {
if (err) {
return res.sendStatus(500).json({
title: 'An error occured',
error: err
});
}
var article = new Article({
title: req.body.title,
description: req.body.description,
body: req.body.body,
username: user.username,
userId: user._id,
favoritesCount: 33,
articleId: req.body._id
// comments: 'bla'
});
article.save(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occured when saving',
error: err
});
}
user.articles.push(result);
console.log(result);
user.save();
res.status(201).json({
message: 'Article saved succesfully',
obj: result
});
});
});
});
// Updating an article // /:id
router.patch('/:id', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
Article.findById(req.params.id, function (err, article) {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
if (!article) {
return res.status(500).json({
title: 'Article not found',
error: { message: 'Article was not found!' }
});
}
if (article.user != decoded.user._id) {
return res.status(401).json({
title: 'Not Authenticated',
error: {
message: 'Users do not match'
}
});
}
article.title = req.body.title,
article.description = req.body.description,
article.body = req.body.body,
article.favoritesCount = 33,
// article.tags = req.body.tags,
article.save(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
res.status(200).json({
message: 'updated succesfully',
obj: result
});
});
});
});
router.delete('/:id', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
Article.findById(req.params.id, function (err, article) {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
if (!article) {
return res.status(500).json({
title: 'Article not found',
error: { message: 'Article was not found!' }
});
}
if (article.user != decoded.user._id) {
return res.status(401).json({
title: 'Not Authenticated',
error: {
message: 'Users do not match'
}
});
}
article.remove(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
res.status(200).json({
message: 'deleted succesfully',
obj: result
});
});
});
})
module.exports = router;
Другие маршруты работают должным образом ...
Это мой сервис, который подключается к API-интерфейсам маршрутов в моем интерфейсе ...
ngOnInit() {
this.getArticleDetail(this.activatedRoute.snapshot.params['articleId']);
}
// ngOnInit() {
// this.articleService.getArticle(this.article)
// .subscribe(article => this.article = article);
// }
getArticleDetail(articleId) {
this.http.get('/article/' + articleId).subscribe(
data => {
this.article = data;
}
);
}
ответ об ошибке в консоли моего браузера ===>
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: «Unauthorized»,url: "http://localhost:7777/article/5b0be8829f734a4e580a43c5", ok: false,…} ошибка: {title:" Not Authenticated ", error: {…}} заголовки: HttpHeaders {normalizedNames: Map (0), lazyUpdate: null, lazyInit: ƒ}сообщение: «Http-сообщение об ошибке для http://localhost:7777/article/5b0be8829f734a4e580a43c5: 401 неавторизовано» name: «HttpErrorResponse» ok: false status: 401 statusText: «Unauthorized» url: «http://localhost:7777/article/5b0be8829f734a4e580a43c5"