perhaps? 2014-09-08 18:28 采纳率: 100%
浏览 44

使用Node.JS进行PUT路由

I am new here and I have a problem, I hope you can help me :).

I have the following AJAX in my client side that sends the data to update the database:

$.ajax({
 type: 'PUT',
 url: host + '/database',
 data {
  name: 'MisterX',
  email: 'mister.x@gmail.com
 },
 sucess:function(sucess) {
  alert('sucess');
 },
 err:function(err) {
  console.log(err);
 }
});

Them, in my routes file i have:

app.put('/database', function(req, res) {
        require('data', function (data){
            var database = data;

            database.save(function (err) {
                if(err) {
                    console.log('err', err);
                }
                res.status(200).json('ok');
                console.log('Database updated');
            });
        });
    });

This put route was completely done in theory, I do not know exactly how to take the data and updates them in my database

My schema:

var databaseSchema = mongoose.Schema({
        name: String,
        email: String,
    });

If someone can give me a direction, I appreciate it.

Thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33726313 2014-09-08 19:09
    关注

    Schema file databaseScheme.js

    ....
    var databaseSchema = mongoose.Schema({
        nome: String,
        email: String,
    });
    ...
    

    route

    /*
     * body-parser is a piece of express middleware that 
     *   reads a form's input and stores it as a javascript
     *   object accessible through `req.body` 
     *
     * 'body-parser' must be installed (via `npm install --save body-parser`)
     * For more info see: https://github.com/expressjs/body-parser
     */
    var bodyParser = require('body-parser');
    var app = express();
    // instruct the app to use the `bodyParser()` middleware for all routes
    app.use(bodyParser());
    
    var Database = require(. / databaseScheme);
    ...
    ...
    // As explained above, usage of 'body-parser' means
    // that `req.body` will be filled in with the form elements
    // Adds a new document
    app.post('/database', function(req, res) {
        var database = new Database();
        database.name = request.body.name;
        database.email = request.body.email;
        database.save(function(err) {
            if (err) {
                console.log('err', err);
            }
            res.status(200).json('ok');
            console.log('Database updated');
        });
    });
    
    // Update a document
    app.put('/database/:id', function(req, res) {
        Database.findById(req.param('id'), function(err, database){
            database.name = request.body.user;
            database.email = request.body.user;
            database.save(function(err) {
                 if (err) {
                    console.log('err', err);
                 }
                 res.status(200).json('ok');
                 console.log('Database updated');
           });
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮