node express routing params not catching - angularjs

I have this
router.put('/user:resourceId', function(req, res) {
User.findOneAndUpdate({resourceId: req.params.resourceId}, req.body, function(err, user) {
if (err)
res.send(err);
res.send(user);
});
});
and in my angular code I have
updateResource: function(resource){
var self = this;
return $http.put('api/resources/resource', resource, {params:{resourceId:resource.resourceId}}).then(function(response){
return response.data;
}, function(err){
$log.error('Error updating', err);
});
}
Why isn't this path catching? It works when I remove the params.

Your route looks a little off, try the following:
router.put('/user/:resourceId', function(req, res) {
...
});
For a URL that looks like: /user/123, where req.param.resourceId is 123.
It looks like your angular code is pointing to /api/resources/resource though, so I can understand why it's perhaps not matching your Express route for /user/:resourceId - (Unless your Express router is set to handle request for something like the following: /api/resources/resource/user/:resourceid?

Related

NodeJs + ExpressJs app routing odd behavior

I am learning ExpressJS. So far I have setup a simple todo app with user authentication using PassportJS. I use Mongoose for repository. There is nothing in the web to explain the odd behavior that I'm seeing with my route setup.
Scenario:
When I hit get /passport it will direct to the passport page
(login/signup)
When I hit get /aslkdjf it will direct to the passport page if
user is not logged in, else it will direct to the file
/public/index.html)
When I hit get / it should direct to the passport page if user
is not logged in, but it goes to /public/index.html instead and
my todo app would fail as req.user.username under /api/todos is
undefiend
Strangely, when I remove the router.get('/*', ... configuration, my app would still go to public/index.html, when I hit base path '/', but not when I hit '/asdfa'.
...
function loggedIn(req, res, next) {
if (req.user) {
next();
} else {
res.redirect('/passport');
}
}
var router = express.Router();
// passport ----------------------------------------------------------------
// get passport page
router.get('/passport', notLoggedIn, function(req, res) {
res.sendfile('./public/passport.html');
});
// post login
router.post('/login', passport.authenticate('login', {
successRedirect: '/',
failureRedirect: '/passport',
failureFlash: true
}));
// post registration
router.post('/signup', passport.authenticate('signup', {
successRedirect: '/',
failureRedirect: '/passport',
failureFlash: true
}));
router.get('/logout', function(req, res) {
req.session.destroy();
req.logout();
res.redirect('/');
});
// api ---------------------------------------------------------------------
// get all todos
router.get('/api/todos', function(req, res) {
// use mongoose to get all todos in the database
Todo.find({owner: req.user.username}, function(err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(todos); // return all todos in JSON format
});
});
// create todo and send back all todos after creation
router.post('/api/todos', function(req, res) {
// create a todo, information comes from AJAX request from Angular
Todo.create({
owner: req.user.username,
text : req.body.text,
done : false
}, function(err, todo) {
if (err)
res.send(err);
// get and return all the todos after you create another
Todo.find({owner: req.user.username}, function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});
// delete a todo
router.delete('/api/todos/:todo_id', function(req, res) {
Todo.remove({
_id : req.params.todo_id
}, function(err, todo) {
if (err)
res.send(err);
// get and return all the todos after you create another
Todo.find({owner: req.user.username}, function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});
// application -------------------------------------------------------------
router.all('*', loggedIn);
router.get('/*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
app.use('/', router);
app.listen(3000);
console.log("App listening on port 3000");
Can someone explain to me what is going on? All I want to achieve is to make the app re-route user to the login page when they are not logged in and they go to www.myapp.com/
So apparently the problem is that HTML defaults to finding an index.html file in any folder within the root. When I change the html file to something else like abc.html the problem is solved. Seems like a bug to me.

NodeJS - Cannot set headers after they are sent - Multiple Calls

I'm trying to write an app that find a city in a MongoDB collection and uses the latitude and longitude it returns to find all zip codes within a certain distance. It seems to work, but the problem is that I'm getting an error that I can't set headers after they've already been sent. However, I've separated the to routes into different requests I don't understand why I'm still getting this error. What is the best way to make multiple calls to the API?
Here is my router in Node/Express:
// route to get city
app.get('/cities/:zip', function(req, res) {
// use mongoose to get the city in the database
console.log(req.params.zip);
var query = City.find({"zip" : req.params.zip});
query.exec(function(err, city) {
if (err)
res.send(err);
res.json(city);
});
});
// route to find cities within 50 miles
app.get('/matches/:latMin/:latMax/:lonMin/:lonMax', function(req, res) {
console.log(req.params.latMin + req.params.latMax + req.params.lonMin + req.params.lonMax);
var matches = City.find({latitude: {$gt: req.param.latMin, $lt:req.params.latMax }, longitude : {$gt :req.param.lonMin, $lt : req.param.lonMax}});
matches.exec(function(err, match){
if(err)
res.send(err);
console.log(match);
res.json(match);
});
});
app.get('*', function(req, res) {
res.sendfile('./public/views/index.html'); // load our public/index.html file
});
Here is my Angular Controller
$scope.update = function (zip) {
City.get({zip : zip}).success(function(response){
$scope.weather = response
}).then(function(response){
$scope.weather = response.data;
})
if(zip.length = 5){
$http.jsonp('http://api.openweathermap.org/data/2.5/weather?zip='+ zip +',us&callback=JSON_CALLBACK&units=imperial').success(function(data){
$scope.data=data;
});
var box = getBoundingBox([$scope.weather[0].latitude, $scope.weather[0].longitude], 50);
City.matches(box[1], box[3], box[0], box[2]).success(function(response){
$scope.matches = response
}).then(function(response){
$scope.matches = response.data;
console.log($scope.matches);
})
}
res.send does not return; the call continues to res.json. And please use braces. Please. Maybe they don't look cool or whatever. Just use them.
if (err) { handleError(res, err); return; }
res.status(200).json(city);
Further down, keeping things DRY:
function handleError(res, err) {
res.status(500).json(err);
}

Mongoose query via Angular variable

I´m trying to query my mongoose model with an angular variable - an email adress.
I´ve got a variable from passport with contains the email <%= user.local.email %>.
The $http.getfor my Model looks like this:
app.get('/profile/project/:mail', function (req, res, next){
Project.findOne(req.params.mail, function (err, doc){
if(err) return next(err);
res.json(doc);
});
});
I can´t figure out how to pass the mail adress with the angular controller, to the GET request.
My try:
$http.get('/profile/project/' + <%= user.local.email %>).then(function (res){
$scope.project = res.data;
});
});
Setting the findOne parameter to a fixed mail adress worked, but I´ve got no idea how to pass the mail adress with the angular controller.
Hope you can help me.
Best regards, David
Update using passport
Getting near the solution, but now I only get the first data entry of my DB.
Sum it up, my route:
app.get('/profile/project/', function (req, res, next){
Project.findOne(req.user.local.email, function (err, doc){
if (err) return next(err);
if (!doc) return next(new Error('cant find'));
console.log(doc);
res.json(doc);
});
});
My angular controller:
<script>
var app = angular.module('myApp', []);
app.controller('projectCtrl', function($scope, $http) {
$http.get('/profile/project/').then(function (res){
$scope.project = res.data;
});
});
</script>
Passport works,I can log in and show the email address on the profile page. But with this route I´m just getting the first [0] entry from my database.
Are you sure you want angular to pass this variable?
If it's in passport, then it's likely the user's own email? If so then you should just look it up entirely on the backend.
If the email is really being passed from angular, then it doesn't make sense to have it in passport at all, and you would just pass it as a js var.
Edit
findOne should only return one result. Maybe you need to use find if you are trying to get multiple records.
Got the solution. Big thanks to #Austin_Greco!
Route:
app.get('/profile/project/', isLoggedIn, function (req, res, next){
Project.findOne({'author' : req.user.local.email}, function (err, doc){
if (err) return next(err);
if (!doc) return next(new Error('cant find'));
console.log(doc);
res.json(doc);
});
});
Angular Controller:
$http.get('/profile/project/').then(function (res){
$scope.project = res.data;
});

Updating database with node.js and angular

I have an app which posts, gets and deletes data and I would like to add 'update' functionality as well but I can't figure it out..
I have a node.js server which has such api:
app.get('/api/feedbacks', function(req, res) {
// use mongoose to get all feedbacks in the database
getfeedbacks(res);
});
// create feedback and send back all feedback after creation
app.post('/api/feedbacks', function(req, res) {
// create a feedback, information comes from AJAX request from Angular
FeedBack.create(req.body, function(err, feedback) {
if (err)
res.send(err);
// get and return all the feedbacks after you create another
getfeedbacks(res);
});
});
// delete a feedback
app.delete('/api/feedbacks/:feedback_id', function(req, res) {
FeedBack.remove({
_id : req.params.feedback_id
}, function(err, feedback) {
if (err)
res.send(err);
getfeedbacks(res);
});
});
and such angular service which speaks to node api:
service.factory('FeedBacks', ['$http',function($http) {
return {
create : function(feedBackData) {
return $http.post('/api/feedbacks', feedBackData);
},
get : function() {
return $http.get('/api/feedbacks');
},
delete : function(id) {
return $http.delete('/api/feedbacks/' + id);
}
}
}]);
That way I can post, get and delete data.
My goal is to add also update function.
What I have tried on node:
// update a feedback
app.put('/api/feedbacks/:feedback_id', function(req, res) {
// edit a feedback, information comes from AJAX request from Angular
FeedBack.put(req.body, function(err, feedback) {
if (err)
res.send(err);
// get and return all the feedbacks after you edit one
getfeedbacks(res);
});
});
on Angular service:
update: function(editFeedId, editedFeed){
return $http.put('/api/feedbacks/' + editFeedId, editedFeed);
}
controller looks like:
$scope.editFeed = function(id) {
$scope.editFeedId = id;
$scope.editedFeed = 'replace this txt'
FeedBacks.update($scope.editFeedId, $scope.editedFeed)
// if successful creation, call our get function to get all the new
feedBacks
.success(function(data) {
console.log('updated');
$scope.feedbacks = data;
});
};
I get 500 error as I execute editFeed(). I couldn't figure out to configure that! Where do I do wrong? Any Tips?
Thanks a lot in advance!
I'm assuming you're using Mongo here, in which case your update statement is incorrect.
It should be something like:
app.put('/api/feedbacks/:feedback_id', function(req, res) {
FeedBack.update({_id: req.params.feedback_id}, req.body, function(err, feedback) {
if (err)
res.send(err);
// get and return all the feedbacks after you edit one
getfeedbacks(res);
});
});

How to implement customGet in restangular?

How to implement customGet from restangular?
I wrote some code in a server like this:
user.controller.js:
exports.getRunner = function(req, res) {
User.find({provider: "instagram"}, function (err, users) {
if (err) return res.send(500, err);
res.json(200, users);
});
};
index.js:
router.get('/getRunner', controller.getRunner);
If I try to get data from postman... it responds the right json data, it works properly.
But when I try using Restangular customGet in my client side, I get instead an undefined error.
This is my client side:
Restangular.all('users').customGET('getRunner').then(function(teams) {
$scope.teams = teams;
});

Resources