CastError:对于值" NaN"转换为ObjectId失败在路径" _id"

时间:2016-11-11 23:05:10

标签: angularjs mongodb express mongoose json-server

[ 问题现在已经解决......答案非常简单......

$ scope.article = articleFactory.getArticles()。get({id:parseInt($ stateParams.id,10)})             。$ promise.then(

应阅读:

$scope.article = articleFactory.getArticles().get({
        id: $stateParams.id
    })

我确实尝试过这个但是由于某种原因,Chrome正在缓存旧代码 - 做了一个清晰的历史,一切都很好。

Big" Doh"。

我在将网站从json-server移动到express / mongo / mongoose时遇到问题。在json上一切正常,但是当我将它移动到express / mongo / mongoose上时会出现问题,这会引发以下错误:

CastError: Cast to ObjectId failed for value "NaN" at path "_id" 

然后服务器崩溃......

我可以通过修改路线来阻止崩溃。以前,就是这样:

articleRouter.route('/:articleId')
.get(function (req, res, next) {
    Articles.findById(req.params.articleId, function (err, article) {
        if (err) throw err;
        res.json(article);
    });

})

但如果我添加一个if语句来过滤掉NaN,那么服务器就会运行:

articleRouter.route('/:articleId')
.get(function (req, res, next) {

if (id.match(/^[0-9a-fA-F]{24}$/)) {
        Articles.findById(req.params.articleId, function (err, article) {
        if (err) throw err;
        res.json(article);
    });
}
})

但它没有提供"详细信息页面" id传递的地方。我非常强烈地怀疑这与mongoose模式中的类型有关,但我对此很新,而且有点迷失。

架构如下 - 我已尝试使用和不使用id字段:

var articleSchema = new Schema({
    _id: {
         type: String,
         required: true,
         unique: true,
         index: true
    },
    headline: {
        type: String,
        required: true,
        unique: false
    }
---blah blah etc---
}

我已经包含了下面的代码,其中可能相关或不相关,但我95%认为这是一个猫鼬的事情。有什么想法吗?

提前致谢 燕姿

相关标记是: (在" news.html")

<div ng-controller="ArticleController">
<div><a ui-sref="app.newsdetail({id: article.id})">See details</a></div>
</div>

标记:(在&#34; newsdetail.html&#34;)

<div ng-controller="ArticleDetailController">
{{article.headline}}
{{article.text}}
</div>

控制器是:

.controller('ArticleController', ['$scope', '$stateParams', 'articleFactory', function ($scope, $stateParams, articleFactory) {
'use strict';
  articleFactory.getArticles().query(
      function(response) {
          $scope.articles = response;
      },
      function(response) {
          $scope.message = "Error: "+response.status + " " + response.statusText;
      }
  );
}])

.controller('ArticleDetailController', ['$scope', '$stateParams', 'articleFactory', function ($scope, $stateParams, articleFactory) {
    $scope.article = {};

    $scope.article = articleFactory.getArticles().get({id:parseInt($stateParams.id,10)})
        .$promise.then(
            function(response){
                $scope.article = response;
                $scope.showArticle = true;
            },
            function(response) {
                $scope.message = "Error: "+response.status + " " + response.statusText;
            }
    );        
}])

服务是:

.service('articleFactory', ['$resource', 'baseURL',      function($resource,baseURL) {
'use strict';
    this.getArticles = function(){ 
        return $resource(baseURL+"articles/:id",null,{'get':{method:'GET' }}); 
    }; 
}]) 

1 个答案:

答案 0 :(得分:1)

在您的架构中,您正在定义_id字段,这是一个&#34;保留&#34;字段,也是您定义String的ObjectId类型。您应该从架构中删除_id,它会自动添加(并编入索引)。 另请参阅Mongoose schema documentation