Organizational Research By

Surprising Reserch Topic

how to call node js api from angular


how to call node js api from angular  using -'jquery,angularjs,node.js,express,mean'

My studentApi.js is as follows, router.param() is used to save code for repeating again and againt.

router.param('post', function (req, res, next, id) {
    var query = Post.findById(id);
    query.exec(function (err, post) {
        if (err) { return next(err); }
        if (!post) { return next(new Error('Can\'t find post')); }
        req.post = post;
        return next();
    })
});
router.put('/posts/:post/upvote', function (req, res, next) {
    res.post.upvote(function (err, post) {
        if (err) { return next(err);}
    });
});


In angular I am calling like

 o.upvote = function (post) {
        return $http.put('/studentapi/posts/' + post._id + '/upvote')
          .success(function (data) {
              alert("post voted");
              post.upvotes += 1;
          });
    };


Error:


My model is as follows, calling upvote method internally from model.

var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
    title: String,
    link: String,
    upvotes: { type: Number, default: 0 },
    downvotes: { type: Number, default: 0 },
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});

mongoose.model('Post', PostSchema);

PostSchema.methods.upvote = function (cb) {
    this.upvotes += 1;
    this.save(cb);
}



    

asked Oct 11, 2015 by rajesh
0 votes
10 views



Related Hot Questions

2 Answers

0 votes

How do I update/upsert a document in Mongoose?

Here is a good start. I personally use the following approach recommended at scotch.io

app.put('url', function(req, res) {

    // use our bear model to find the bear we want
    Bear.findById(req.params.bear_id, function(err, bear) {

        if (err)
            res.send(err);

        bear.name = req.body.name;  // update the bears info

        // save the bear
        bear.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Bear updated!' });
        });

    });
});

https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

answered Oct 11, 2015 by sujata naik
0 votes

Thanks for all the help it was really appreciated, Actually there was a mistake in Mongoose Model in my case posts.js as I need to attach the model after defining method in Post model. The correct one was

var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
    title: String,
    link: String,
    upvotes: { type: Number, default: 0 },
    downvotes: { type: Number, default: 0 },
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});

PostSchema.methods.upvote = function (cb) {
    this.upvotes += 1;
    this.save(cb);
}

mongoose.model('Post', PostSchema);

As now I put PostSchema.methods.upvote stuff above the mongoose.model('Post',PostSchema);

answered Oct 11, 2015 by ashish singh

...