2014-03-19 55 views
0

我遵循這個樣板代碼(https://github.com/jkat98/benm)我已經得到了結構的好處,我喜歡它。作者沒有向MongoDB服務器提供更新方法/ PUT請求。他說這一步稍微複雜一點,所以我希望能向您展示發送數據到服務器的主要路由和邏輯,並可能幫助實現PUT請求。用JavaScript做出一個請求?

所以在我看來,這裏有3或4個關鍵的文件,使魔術發生。

A.在app/models.js中,定義了mongodb的模型。

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    ObjectId = Schema.ObjectId; 

var Message = new Schema({ 
    username: { type: String }, 
    message:  { type: String }, 
    connected: { type: Boolean }, 
    sent:  { type: Date, default: Date.now } 
}); 

module.exports = { 
    Message: mongoose.model('Message', Message) 
}; 

B.在app/routes.js中創建api的路由。

var home = require('../controllers/home'), 
    messages = require('../controllers/messages'); 

module.exports.initialize = function(app) { 
    app.get('/', home.index); 

    // Messages 
    app.get('/api/messages', messages.index); 
    app.get('/api/messages/:id', messages.getById); 
    app.post('/api/messages', messages.add); 

    // Having a hard time with the PUT request 
    app.put('/api/messages', messages.update); 

    app.delete('/api/messages/:id', messages.delete); 
}; 

C.在controllers/messages.js控制器。

var models = require('../app/models'), 
    md5 = require('MD5'); 

module.exports = { 
    index: function(req, res) { 
     models.Message.find({}, function(err, data) { 
      res.json(data); 
      console.log(data); 
     }); 
    }, 
    getById: function(req, res) { 
     models.Message.find({ _id: req.params.id }, function(err, message) { 
      if(err) 
       res.json({error: 'Message not found.'}); 
      res.json(message); 
     }); 
    }, 
    add: function(req, res) { 
     var newMessage = new models.Message(req.body); 
     newMessage.save(function(err, message) { 
      if (err) 
       res.json({}); 
      console.log('successfully inserted message: ' + message._id); 
      res.json(message); 
     }); 
    }, 

    // Again hard time with this method 
    update: function(req, res) { 
     console.log(req.body); 
     models.Message.update({ _id: req.body.id }, req.body, function(err, updated) { 
      if (err) { 
       res.json({error: 'Message not found.'}); 
      } else { 
       res.json(updated); 
      } 
     }) 
    }, 

    delete: function(req, res) { 
     models.Message.findOne({ _id: req.params.id }, function(err, message) { 
      if (err) { 
       res.json({error: 'Message not found.'}); 
      } else { 
       message.remove(function(err, message){ 
        res.json(200, {status: 'Success'}); 
       }) 
      } 
     }); 
    } 
}; 

D.最後一些邏輯來在client/src/views/I_HAVE_A_CUSTOM_VIEW.js

// Okay so my code is slightly different than the boilerplate but the idea is to 
// update a model in the collection kind of like the method below 
// the method below does fire off a PUT request, but returns an error since 
// my update/PUT logic is not correct 
createMessage: function(data) {  

    var model = window.App.data.messages.where({username: data.username}) 
    _.each(model, function(model) { 
     console.log(model.get('_id')); 
     model.set({message: data.message}) 
     model.save({}, {url:'/api/messages/'+model.get('_id')}) 
    }); 

    //window.App.data.messages.create(newMessage); 
    window.App.core.vent.trigger('app:log', 'Add View: Saved new message!'); 
} 

發送PUT請求所以這可能是一個棘手的問題,但基本上我只需要一點點幫助搞清楚如何寫更新方法,這樣我就可以在mongodb中成功更新文檔。

編輯:筆者說,他不執行PUT請求,爲什麼在這裏https://github.com/jkat98/benm/issues/12

回答

0

你只需要添加要更新,哪些字段。像這樣(我只是更新用戶名):

update: function(req, res) { 
     console.log(req.body); 
     models.Message.update({ _id: req.body.id }, req.body, function(err, message) { 
      if (err) { 
       res.json({error: 'Message not found.'}); 
      } else { 
       message.username = req.body.username; 
       message.save(); 
       res.json(message); 
      } 
     }) 
    },