node.js - Express.get is getting variable name instead of it's value from AngularJS ui-router -
as mention in title, when send param url using ui-router, , try use express.get(url), :param name instead of it's value.
here code:
angularjs ui-router state:
.state('post', { url: '/blog/:postid', templateurl: 'app/views/post.html', controller: 'postctrl' })
angularjs http request
$http.get('/blog/:postid').success(function(data) { console.log(data); $scope.singlepost = data; }).catch(function(error) { console.log(err); });
html ui-router url
<h1><a ui-sref="post({ postid: post._id })">{{ post.title }}</a></h1>
express.get() method
app.get('/blog/:id', function(req, res) { post.findbyid(req.params.id, function(err, singlepost){ console.log(singlepost); if (err) { console.log(err); } else { res.json(singlepost); } }); });
i following error in console
{ [casterror: cast objectid failed value ":postid" @ path "_id"] message: 'cast objectid failed value ":postid" @ path "_id"', name: 'casterror', kind: 'objectid', value: ':postid', path: '_id', reason: undefined }
any and/or explanation of why it's happening newbie appreciated. thank you.
i inferring here trying make ajax request angular application inside postctrl controller.
.state('post', { url: '/blog/:postid', templateurl: 'app/views/post.html', controller: function($scope, $stateparams) { var postid = $stateparams.postid; } })
in controller can make ajax request whatever have received.
var req = { method: 'post', url: 'http://localhost:3300/blog', headers: { 'content-type': 'application/x-www-form-urlencoded' }, data: $httpparamserializer(postid) }; $http.post(req).then(function(success){},function(error){});
your complete state be:
.state('post', { url: '/blog/:postid', templateurl: 'app/views/post.html', controller: function($scope, $stateparams) { var postid = $stateparams.postid; var req = { method: 'post', url: 'http://localhost:3300/blog', headers: { 'content-type': 'application/x-www-form-urlencoded' }, data: $httpparamserializer(postid) }; $http.post(req).then(function(success){},function(error){}); } })
of course need add dependencies $httpparamserializer
Comments
Post a Comment