1

When I try to change the status of a blog , the status is not updating in database. Status is string field and is initially stored as 0 in database

api.post('/statuschange', function(req, res){
    Blog.find({_id: req.query.blog_id}).update({$set:{'status': req.body.status}}, function (err, status) {

            if(err){
                res.send(err);
                return;
            }

            if(req.body.status == '1') {
                res.json('Blog added')
                    return;
                }

            if(req.body.status == '-1'){
                 res.json('Blog not added');
                     return;
                 }
    });
})

api is working successfully on postman

factory file

angular.module('blogfact', ['authService'])

.factory('blogFactory', function($http, AuthToken){

var factory = {};
var token = AuthToken.getToken();

factory.changestatus = function(info, callback){

     $http({
        url: 'api/statuschange',
        method:'POST',
        headers:{'x-access-token':token},
        params:{'blog_id': info},
    })
        .success(function(response){
            callback(response)
        })
}
return factory
})

the controller file

angular.module('blogCtrl', ['blogfact']);

.controller('BlogController', function(blogFactory, $routeParams){

    var that=this;
    blogid = $routeParams.id;
    var getthatBlog = function(){
        blogFactory.getthatBlog(blogid, function(data){
        //console.log('[CONTROLLER] That Blog:',data);
        that.blog = data;
    })
}
    this.changestatus = function(info){
        blogFactory.changestatus(info, function(data){
            getthatBlog();
        })
        }
    })

html file

<div ng-controller="BlogController as blog">
<textarea ng-model="blog.status"></textarea>
<button class="btn btn-success" ng-click="blog.changestatus(blog._id)">Submit</button>
</div>
4
  • Have you watched your browser's console for any client/server-side error. Commented Jun 6, 2016 at 5:12
  • Your question is a bit unclear. Surely it's not Angular that is "updating the database"? Do you perhaps have a problem with the data being sent? Or is it a problem with server side code? (Also, it'd help if you edit the question and format the code for readability.) Commented Jun 6, 2016 at 5:14
  • @AhmadBaktashHayeri hi.. I am new to MEAN.. but whenever i go to the html page for changing the status.. the browser console show the info of blog which is returning from the blogfactory Commented Jun 6, 2016 at 5:15
  • @Jeroen i couldn't understand that how is data sent to html file or what should i pass in html file to update the status.. the api is working fine postman Commented Jun 6, 2016 at 5:19

3 Answers 3

1

If your question is regarding the value in MongoDB not being updated, well it seams it is due to the status data missing in your POST request.

I recommend that in your HTML, send the whole blog object so that you have the blog's status as well:

<button class="btn btn-success" ng-click="blog.changestatus(blog)">Submit</button>

Then in your blogFactory add the data as such:

$http({
    url: 'api/statuschange',
    method:'POST',
    headers:{'x-access-token':token},
    params:{'blog_id': info._id},
    data: {status: info.status}   // <==
})

Now, you should be able get the blog status data in NodeJS server back-end via req.body.status.

Update

Try the following with mongoose's update method:

Blog.update({_id: req.query.blog_id}, {status: req.body.status}, function(err, numAffected){
   ...
});

Or, alternatively:

Blog.findOne({_id: req.query.blog_id}, function(err, blog){
    blog.status = req.body.status;
    blog.save();
});
Sign up to request clarification or add additional context in comments.

4 Comments

still not working.. in console.log(req.body) in api .. the console is showing {status: the value which i entered(1 or 0 or -1).. but is not updated in my db
This means at least you are getting the status in the body. Better log the err as well to see if there is any DB error while updating. If you're using mongoose, it's preferable to use .update instead of your approach.
yes i'm using mongoose and can't understand where the error is..!!
A tip: Try a mongoosish approach as in an update to my answer and confirm.
0

Angular let's you modify collection data on the client-side, but to actually update it on your server you need to notify the server of your changes (via API).

There are a few ways to do this, but if you want seamless updating from client-side to server maybe give meteor a try.

http://www.angular-meteor.com/

https://www.meteor.com/

Comments

0
  1. Your are sending the data in params and getting the data from req.body. You should use req.query or req.param. Else, Send the data on body like below,

    $http({ url: 'api/statuschange', method: 'POST', params: { 'blog_id': info }, data: { 'status': 1 } })

  2. Your are passing 1 parameter in client side and getting two parameters on server side(req.body.status, req.query.blog_id)

  3. Where is the token value from ?

Check the simplified way to test your code http://plnkr.co/edit/tyFDpXw2i0poICwt0ce0

11 Comments

the problem still persists..!! i am not understanding that how the new value (which i am entering) is being sent
change this.changestatus to that.changestatus
Had you checked that you're getting value on node.js ?
i don't know what exactly what you are trying to say.. the api is working successfully in postman
Just print console.log(req.body) inside your function api.post('/statuschange', function(req, res){ and then restart the server and then click Submit button, Check what value your getting.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.