Hi all, thanks in advance for any help you can give me.
I have a route in my api which will up-vote a mode schema already created. I want the express app.get to send a message to my Ionic app to set a message on my $ionicPopup. Something just saying your vote has been accepted. Any suggestions will be greatly appreciated.
First, you need to send a request from your app to your backend with the $http-service:
https://docs.angularjs.org/api/ng/service/$http
$http({
method: 'GET',
url: 'YOUR HOST AND ENDPOINT'
}).success(function (res) {
// success callback
-> open popup with text or something you want to do
}).error(function () {
// request failed
})
So on the success I can do something like .succcess var popUpmsg = "It worked?"
How do I inject this into $ionicPopup?
I could have angular do $http.get but I am not setting my response message as part of the json being produced by the Api. I just want a simple success or failed message like Flash Message.
http://ionicframework.com/docs/api/service/$ionicPopup/
$http({
method: 'GET',
url: 'YOUR HOST AND ENDPOINT'
}).success(function (res) {
// success callback
var alertPopup = $ionicPopup.alert({
title: 'Vote',
template: 'Your vote has been accepted'
});
alertPopup.then(function (res) {
console.log('popup has been closed');
});
}).error(function () {
// request failed
})
Thanks drastick. What if the app.get is really just a vote up? LIke I have no need to do an $http request really. I just want a link that will hit the node server. Then the node server will do a voteup function and return a success. So at this point I do not want to return any json from the server just a simple href to a app.get which will trigger the vote up function and return a succes flash message.
maybe you do not understand how server app communication works?
you have a vote button -> put a ng-click function on that button or other dom node -> call a scope function. this function sends a put or post request to vote up -> and if it succeeded, show the popup like @drastick showed you.
Right now I have it set up so that when node receives a connection to that route it does a mongoose function to update the mongodb collection, what I am trying to do is and upvote link so Mongoose uses findoneandupdate with $in +1. So instead I should to a http.put? Could you show me a example code or point to a tutorial? Thanks, Im pretty new at this.
I need to know what you vote…
You have a model with documents in it… and you vote 1 document?
Like you model name is “post”
then i would do something like this:
endpoint: PUT /api/v1/post/[postid]/vote
in your frontend you click the button -> that sends a request to this endpoint
in this endpoint ein would load the document with the postid
PostCollection.findById(postId, function (err, post) {
//increment vote
post.votes = post.votes + 1;
poste.save(function (err, saved) {
// send saved document to frontend as result (toObject transforms mongo object to ordinary javascript object)
res.send(saved.toObject());
});
});
In other cases i need more information about your backend and api.
And if you get the success callback to your frontend show the popup.