如果在数据库中找不到object_id,则重定向url路径

时间:2017-08-21 17:38:40

标签: angularjs node.js mongodb express mongoose

如果我api.js res.json({ success:falsse })elseres.redirect('...')更改404 error,当我更改数据库中现有对象的网址路径时,没有任何反应。我的问题是使用angular处理404 error的正确方法,或者我应该使用erroro中间件在服务器端处理 router.get('/seeMore/:id', function(req, res) { var seeMore = req.params.id; if(objectid.isValid(seeMore)){ Product.findOne({_id:req.params.id}, function(err, product) { if(err) throw err if(!product) { res.json({ success: false, message: 'Product no found' }); } else { res.json({ success: true, product: product }); } }) } else { res.json({ success:false }) } });

api.js

.when('/register', {
    templateUrl: 'app/views/pages/users/register.html',
    controller: 'regCtrl',
    controllerAs: 'register',
    authenticated: false
})
.when('/home', {
    templateUrl: 'app/views/pages/users/home.html',
    controller: 'mainCtrl',
    controllerAs: 'main',
    authenticated: true,
})
.when('/seeMore/:id', {
    templateUrl: 'app/views/pages/users/nawigacja/zobaczwiecej.html',
    controller: 'seeMoreCtrl',
    controllerAs: 'seeMore',
    authenticated: [true, false],
})

routes.js

.controller('seeMoreCtrl', function($scope,$routeParams,User, $window){ 

var app = this;
if(data.data.success){
    User.readMoreCourse($routeParams.id).then(function(data){
        app.currentProduct = data.data.product._id;
        console.log(data.data.product)
    });
} else {
    $window.location.assign('/register')
}

controller.js

.factory('User', function($http){
userFactory = {}

userFactory.readMoreCourse = function(id) {
    return $http.get('/api/zobaczwiecej/' + id) 
}
return userFactory

});

服务

Course.includes(:content_picture).where("content_picture.id = ?", content_picture_id)

})

1 个答案:

答案 0 :(得分:0)

在controller.js中,为什么你只能使用$ location.path(' / register')

controller.js

.controller('seeMoreCtrl', function($scope,$routeParams,User, $window){ 

    var app = this;
    if(data.data.success){
        User.readMoreCourse($routeParams.id).then(function(data){
            app.currentProduct = data.data.product._id;
            console.log(data.data.product)
        });
    } else {
            $location.path( "/register" ); //------------> change here
    }