蒙戈节点REST服务发布 [英] Node Mongo REST Service Post

查看:162
本文介绍了蒙戈节点REST服务发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了如何使用Node.js和MongoDB用在我的Andr​​oid应用程序的一个REST服务。
get方法正按预期从Android应用程序调用时。
我试图建立从Android应用程序与必要的细节,并发布到网上service.I JSON对象我无法张贴到我的收藏品。下面是code片断我在我的Node.js使用

I have created a REST service using Node.js and MongoDB for using in one of my Android app. The get methods is working as expected when called from Android App. I am trying to build a JSON object from Android App with necessary details and post to the service.I am unable to post to my collections. Below is the code snippet I have used in my Node.js

app.post('/accounts/put/:uObject', function(req, res, next) {
  var username=uObject.name;

  db.collection('test').insert({"username":username},function(err,docs){if(err){
                res.send("There was some problem during insertions of linkes");
            }
           else{
                res.send("Fail");

           } });

});

我在做什么错在这?我得到的对象作为参数,并获取函数内的值,并传递给插入。

What Am I doing wrong in this? I am getting the object as parameter and getting the values inside the function and passing to the insert.

推荐答案

要得到你需要使用路由参数 req.params ,你的情况 req.params.uObject 。此外,在JavaScript的常见做法是在被满足的条件,以提前返回。

To get the parameter from the route you need to use req.params, in your case req.params.uObject. Also common practice in JavaScript is to return early upon a condition being met.

app.post('/accounts/put/:uObject', function(req, res, next) {
    var username = req.params.uObject;

    db.collection('test').insert({
        "username": username
    }, function(err, docs) {
        if (err) {
            return res.send("There was some problem during insertions of linkes");
        }
        res.send("Fail");
    });

});

这篇关于蒙戈节点REST服务发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆