Issue
I have a route in my web app where I'm trying to pass a username via route param. Here's my script.js attached to my index.html:
$scope.del_movie = function(movie) {
$http( {
method: 'DELETE',
url: '/movie/:title/:username',
params: {'title': movie.title, 'username': movie.username}
}).then(function successCallback(response) {
console.log(response);
return getData();
}, function errorCallback(response) {
console.log('Error: ' + response);
});
};
And the route I have set up in my server.js looks like this:
app.delete('/movie/:title/:username', requiresAuth(), function(req, res) {
paramsUsernameString = req.params.username;
oidcEmailString = JSON.stringify(req.oidc.user.email);
console.log("movie " + req.params.username);
if(paramsUsernameString != oidcEmailString){
console.log("req.params.username " + paramsUsernameString + " req.oidc.user.username " + oidcEmailString);
console.log("can't delete someone else's review!");
res.json({
message: "You can't delete someone else's review, as much as you may like to"
});
}
else{
Movie.findOneAndRemove(req.query, function(err, result) {
if ( err ) throw err;
res.json( {
message: "req.params.username " + paramsUsernameString + " req.oidc.user.username " + oidcEmailString,
movie: result
});
});
}
});
The issue is, when I console.log req.params.username, it doesn't come up as the username I passed in with del_movie. It shows up as ":username". I looked up express routing, and I'm stumped as to what I'm doing wrong; my route looks like all the examples, unless I'm missing something obvious.
I've required express, bodyParser, and express-openid-connect, as well as my Movie schema in model.js, which has both a username and a title.
Grateful for any answers, and glad to provide more info/context if needed!
Solution
According to https://docs.angularjs.org/api/ng/service/$http
params – {Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.
That means your url now becomes:
`/movie/:title/:username?title=${movie.title}&username=${movie.username}`
So it is correct that at your server, req.params.username will be that hardcoded ":username".
If you don't want to modify the route, then it should be req.query.username that you want.
Answered By - Old Geezer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.