【问题标题】:Building a REST API using node & Express.js, my PUT method doesn't work for some reason. Can anyone help me please?使用 node 和 Express.js 构建 REST API,我的 PUT 方法由于某种原因不起作用。任何人都可以帮助我吗?
【发布时间】:2014-11-02 07:14:14
【问题描述】:

我的数据模型:

var UserDetail = new Schema({
      username: String,
      password: String
    }, {
      collection: 'userInfo'
    });
var UserDetails = mongoose.model('userInfo', UserDetail);

这是我在路由文件中的 post 方法

//POST
router.post( '/api/users', function( req, res ) {
    var user = new UserDetails({
        username: req.body.username,
        password: req.body.password
    });
    console.log(req.body.username);
    user.save( function( err ) {
        if( !err ) {
            console.log( 'created' );
            return res.send( user );
        } else {
            console.log( err );
            return res.send('ERROR');
        }
    });
});

我的 get api 完美运行。 josn 看起来像这样:

{"username":"exmpleUsername","password":"examplePassword","_id":"54091d9df8f00fb42055b6f8","__v":0}

但是当我尝试使用 POSTMAN 发布时,它不会添加用户名和密码。它只是像这样添加新的_id:

{"_id":"540e394f37706f701020cf19","__v":0}

提前致谢!

【问题讨论】:

    标签: node.js mongodb express mongoose postman


    【解决方案1】:

    请检查您的 Schema Class 用户名和密码是否是私有的

    var Schema = function(){
    
        var self = this;
    
        var username = ""; // mean private
    
        self._id = ""; // mean public
    
    };
    

    【讨论】:

    • 谢谢,但是“_id”是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多