使用Node.JS进行PUT路由


                    

我是新来的,但我有问题,希望您能帮助我:)。</ p>

我的客户端中有以下AJAX,用于发送数据以更新数据库:</ p>

  $。ajax({
  类型:“ PUT”,
  网址:主机+'/数据库',
  数据{
   名称:“ MisterX”,
   电子邮件:'mister.x@gmail.com
  },
  成功:功能(成功){
   警报('成功');
  },
  err:function(err){
   console.log(err);
  }
});
</ code> </ pre>

他们,在我的路线文件中,我有:</ p>

  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('数据库已更新');
             });
         });
     });
</ code> </ pre>

这种放置路线在理论上是完全完成的,我不知道确切如何获取数据并在数据库中更新它们</ p>

我的模式:</ p>

  var databaseSchema = mongoose.Schema({
         名称:字符串,
         电子邮件:字符串,
     });
</ code> </ pre>

如果有人可以给我指示,我会感激的。</ p>

谢谢。</ p>
     </ div>

展开原文

原文

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个回答


架构文件databaseScheme.js </ p>

 ...。
var databaseSchema = mongoose.Schema({
     Nome:字符串,
     电子邮件:字符串,
});
...
</ code> </ pre>

路线</ p>

  / *
  *人体分析器是一种表达中间件,
  *读取表单的输入并将其存储为javascript
  *可通过req.body访问的对象
  *
  *必须安装'body-parser'(通过npm install --save body-parser
  *有关更多信息,请参见:https://github.com/expressjs/body-parser
  * /
var bodyParser = require('body-parser');
var app = express();
//指示应用对所有路由使用bodyParser()中间件
app.use(bodyParser());

var Database = require(./ databaseScheme);
...
...
//如上所述,“ body-parser”的使用意味着
//req.body将使用表单元素填充
//添加一个新文档
app.post('/ database',function(req,res){
     var database = new Database();
     database.name = request.body.name;
     database.email = request.body.email;
     database.save(function(err){
         如果(错误){
             console.log('err',err);
         }
         res.status(200).json('ok');
         console.log('数据库已更新');
     });
});

//更新文件
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){
              如果(错误){
                 console.log('err',err);
              }
              res.status(200).json('ok');
              console.log('数据库已更新');
        });
   });
});
</ code> </ pre>
     </ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐