2015-05-04 56 views
1

我創建使用貓鼬一個簡單的演示,它完美的作品在localhost:MongoDB的車輛定位系統與貓鼬不插入

mongoose.connect('mongodb://localhost/mindmap', function(err, next) { 
    if(err) return next(err); 
    console.log('database connected'); 
}); 

它獲取的數據與一個簡單的查找:

/* GET mindmaps. */ 
router.get('/', function(req, res, next) { 
    Mindmap.find(function (err, mindmaps) { 
    if (err) return next(err); 

    res.json(mindmaps); 
    }); 
}); 

並通過添加數據插座連接

// handle adding new mindmaps 
    socket.on('mindmap.add', function (data) { 
    Mindmap.create(data, function (err, mindmap) { 
     if (err) throw err; 

     mindmaps.push(mindmap); 
     io.sockets.emit('mindmap.add', mindmap); 
    }); 
    }); 

現在所有這一切都在本地正常工作。

今天,我在CentOS 6.5 VPS上安裝了npm,bower,應用程序本身和MongoDB。我開始蒙戈服務,並在服務器上更改app.js連接到正確的IP /端口:

mongoose.connect('mongodb://127.0.0.1:27017/mindmap', function(err, next) { 

當我啓動應用程序與node bin/www的連接不拋出任何錯誤,只是指出,該數據庫是連接的。/mindmap給出一個空數組(這是邏輯的)。通過套接字連接添加新的思維導圖甚至會發出一個合適的mindmap對象,並且html將顯示它。沒有錯誤,但也沒有任何東西被保存,刷新後它又開始空了。同樣在mongo shell中,集合中沒有任何內容..如果我手動添加某些內容,我無法在/ mindmap中看到它。

完全客戶端代碼是http://mindmap.solidwebcode.com:3000/mindmap

供參考,這是模型:

var mongoose = require('mongoose'); 

var SubjectSchema = new mongoose.Schema({ 
    title: String, 
    x: Number, 
    y: Number, 
    w: Number, 
    h: Number, 
    parent: mongoose.Schema.Types.ObjectId 
}); 

var MindmapSchema = new mongoose.Schema({ 
    name: String, 
    theme: String, 
    subjects: [SubjectSchema], 
    created_at: { type: Date, default: Date.now }, 
    updated_at: { type: Date, default: Date.now } 
}); 

module.exports = mongoose.model('Mindmap', MindmapSchema); 

我使用安裝的MongoDB:

sudo yum --enablerepo=epel install mongodb mongodb-server 
sudo service mongod start 

也開始mongo作品如預期,雖然有一些警告:

Server has startup warnings: 
2015-05-04T13:42:08.955+0200 [initandlisten] 
2015-05-04T13:42:08.955+0200 [initandlisten] ** WARNING: You are running in OpenVZ which can cause issues on versions of RHEL older than RHEL6. 
2015-05-04T13:42:08.955+0200 [initandlisten] 

我從來沒有在VPS上安裝過mongo並使用過它。我錯過了一些微不足道的東西嗎?

回答

0

問題不是MongoDB不能正常工作,而是套接字連接已啓動到localhost:3000,這與外部服務器不同。因此,在遠程服務器上沒有任何事情發生時,本地服務器是接受套接字連接並在其自己的數據庫中添加新記錄。

解決方案很簡單:將套接字連接到遠程地址。