如何使用带有Angularjs的mongoose通过ID获取文档

时间:2018-04-12 14:24:05

标签: angularjs mongodb mongoose

我正在尝试使用mongoose和Angularjs(学习Angular CLI)从$ http请求获取ID的单个文档。任何帮助将不胜感激。

我可以获取id,但无法弄清楚如何将其传递给路由

单篇文章视图控制器:

let _id = $routeParams;


console.log(_id);

$http({
    cache: true,
    method: 'GET',
    url: '/article/:id',
    params: { _id }
}).then(function successCallback(res) {
    $scope.article = res.data;
}).catch(function errorCallback(err) {
    if (err) {
        alert('THERE WAS AN ERROR');
    }
})

HTML:

        <a id="modalButton" ng-href="/articles/{{article._id}}">READ MORE...</a>

猫鼬路由:

router.get('/:id', function(req, res, next){
Article.findById(req.params.id, function(err, article){
        if(err) {
        return err;
     } else {
        res.json(article);
        console.log(article);
   } 
});
});

文章计划:

var ArticleSchema = new mongoose.Schema({
image: String,
title: String,
about: String,
article: String,
_id: String
});

module.exports = mongoose.model('Article', ArticleSchema);

我真的很感激任何帮助。这给我带来了一个星期的麻烦。

0 个答案:

没有答案