[问题现已解决...答案非常简单...
$scope.article = articleFactory.getArticles().get({id:parseInt($stateParams.id,10)}) .$promise.then(
应该阅读:
$scope.article = articleFactory.getArticles().get({
id: $stateParams.id
})
我确实尝试过,但由于某种原因,Chrome 正在缓存旧代码 - 做了一个清晰的历史记录,一切都很好。
大“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 的“详细信息页面”。我非常怀疑这与猫鼬模式中的类型有关,但我对此很陌生,有点迷茫。
架构如下 - 我已经尝试过使用和不使用 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% 认为这是 Mongoose 的事情。有任何想法吗?
在此先感谢史蒂夫
相关标记是:(在“news.html”中)
<div ng-controller="ArticleController">
<div><a ui-sref="app.newsdetail({id: article.id})">See details</a></div>
</div>
标记:(在“newsdetail.html”中)
<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' }});
};
}])