Jquery 如何从angular调用node.js api?

Jquery 如何从angular调用node.js api?,jquery,angularjs,node.js,express,mean,Jquery,Angularjs,Node.js,Express,Mean,我的studentApi.js如下所示,router.param()用于保存代码,以便再次重复 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

我的studentApi.js如下所示,router.param()用于保存代码,以便再次重复

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);}
    });
});
我打电话来是想

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

我的模型如下,从模型内部调用upvote方法

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);
}

这是一个好的开始。我个人使用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!' });
        });

    });
});

这是一个好的开始。我个人使用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!' });
        });

    });
});

这是一个好的开始。我个人使用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!' });
        });

    });
});

这是一个好的开始。我个人使用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!' });
        });

    });
});

感谢所有的帮助,非常感谢,事实上,我的案例posts.js中的Mongoose模型有一个错误,因为我需要在Post-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' }]
});

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

mongoose.model('Post', PostSchema);

现在我把PostSchema.methods.upvote东西放在mongoose.model('Post',PostSchema)之上

感谢大家的帮助,非常感谢,事实上,在我的案例posts.js中,Mongoose模型中有一个错误,因为我需要在Post模型中定义方法后附加模型。正确的答案是

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);

现在我把PostSchema.methods.upvote东西放在mongoose.model('Post',PostSchema)之上

感谢大家的帮助,非常感谢,事实上,在我的案例posts.js中,Mongoose模型中有一个错误,因为我需要在Post模型中定义方法后附加模型。正确的答案是

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);

现在我把PostSchema.methods.upvote东西放在mongoose.model('Post',PostSchema)之上

感谢大家的帮助,非常感谢,事实上,在我的案例posts.js中,Mongoose模型中有一个错误,因为我需要在Post模型中定义方法后附加模型。正确的答案是

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);

现在我把PostSchema.methods.upvote东西放在mongoose.model('Post',PostSchema)之上

您的api是否已启动并运行?而在studentApi.js中只有'post/:post',其中是用“/studentApi”初始化它的代码?可能是您缺少一些初始化。尝试在没有“studentapi”的情况下打开“/posts/”。或者您有“/studentApi”而不是“studentApi”是的,api已经启动并运行,已经在处理简单的get请求。我从db获得记录并成功绑定。在本例中,我正在更新记录,并如上所述传递了参数,但404.studentApi.js似乎是通过节点在服务器端执行的,因此除非导入同名函数,否则alert()不应存在。那么,res.post是什么,它是mongodb链接的模型吗?最后,您在节点控制台中看到了什么?好的,您正在提供“get”路由。put路由在哪里?我的意思是,如果你只是为那个URL定义了一个“GET”,那么你就不能进行HTTP PUT。在req.post中存储一些东西将不会起任何作用,因为在页面加载后,req将被销毁,res.post不存在,它不能这样工作。您必须传递id并重新找到id为的帖子。使用req.params.post您的api是否已启动并正在运行?而在studentApi.js中只有'post/:post',其中是用“/studentApi”初始化它的代码?可能是您缺少一些初始化。尝试在没有“studentapi”的情况下打开“/posts/”。或者您有“/studentApi”而不是“studentApi”是的,api已经启动并运行,已经在处理简单的get请求。我从db获得记录并成功绑定。在本例中,我正在更新记录,并如上所述传递了参数,但404.studentApi.js似乎是通过节点在服务器端执行的,因此除非导入同名函数,否则alert()不应存在。那么,res.post是什么,它是mongodb链接的模型吗?最后,您在节点控制台中看到了什么?好的,您正在提供“get”路由。put路由在哪里?我的意思是,如果你只是为那个URL定义了一个“GET”,那么你就不能进行HTTP PUT。在req.post中存储一些东西将不会起任何作用,因为在页面加载后,req将被销毁,res.post不存在,它不能这样工作。您必须传递id并重新找到id为的帖子。使用req.params.post您的api是否已启动并正在运行?而在studentApi.js中只有'post/:post',其中是用“/studentApi”初始化它的代码?可能是您缺少一些初始化。尝试在没有“studentapi”的情况下打开“/posts/”。或者您有“/studentApi”而不是“studentApi”是的,api已经启动并运行,已经在处理简单的get请求。我从db获得记录并成功绑定。在本例中,我正在更新记录,并如上所述传递了参数,但404.studentApi.js似乎是通过节点在服务器端执行的,因此除非导入同名函数,否则alert()不应存在。那么,res.post是什么,它是mongodb链接的模型吗?最后,您在节点控制台中看到了什么?好的,您正在提供“get”路由。put路由在哪里?我的意思是,如果你只是为那个URL定义了一个“GET”,那么你就不能进行HTTP PUT。在req.post中存储一些东西将不会起任何作用,因为在页面加载后,req将被销毁,res.post不存在,它不能这样工作。您必须传递id并重新找到id为的帖子。使用req.params.post您的api是否已启动并正在运行?而在studentApi.js中只有'post/:post',其中是用“/studentApi”初始化它的代码?可能是您缺少一些初始化。尝试在没有“studentapi”的情况下打开“/posts/”。或者您有“/studentApi”而不是“studentApi”是的,api已经启动并运行,已经在处理简单的get请求。我从db获得记录并成功绑定。在本例中,我正在更新记录,并如上所述传递了参数,但404.studentApi.js似乎是通过节点在服务器端执行的,因此除非导入同名函数,否则alert()不应存在。那么,res.post是什么,是m吗