2016-03-06 49 views
0

我正在聊天室中,用戶可以在項目基礎上過濾彼此聊天。來自同一個項目的用戶可以相互交談。在節點/快遞中推送到mongodb的二級數組

這裏是我的聊天模式,每個文件是基於項目編號,並具有與用戶REFFERENCE消息數組:

'use strict'; 
var mongoose = require('bluebird').promisifyAll(require('mongoose')); 
var ChatSchema = new mongoose.Schema({ 
    projectid: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Project' 
    }, 
    messages: [{ 
    userid: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'User' 
    }, 
    message: String, 
    date: { 
     type: Date, 
     default: Date.now 
    }, 
    time: String 
    }] 
}); 
export default mongoose.model('Chat', ChatSchema); 

現在我嘗試更新的消息數組新郵件,但我過去幾個小時無法這樣做。這是我到目前爲止。

要獲取聊天基於項目的消息,我使用:

路線:

router.get('/projectid/:id', controller.showByProject); 
router.post('/projectid/:id', controller.insertMessageByProject); 

控制器:

// Gets the chat thread based on project id 
export function showByProject(req, res) { 
    Chat.findAsync({projectid: req.params.id}) 
    .then(handleEntityNotFound(res)) 
    .then(respondWithResult(res)) 
    .catch(handleError(res)); 
} 

// Insert a new message in the chat based on projectid 
export function insertMessageByProject(req, res) { 
    if (req.body._id) { 
    delete req.body._id; 
    } 
    Chat.findAsync({projectid: req.params.id}) 
    .then(handleEntityNotFound(res)) 
    .then(saveUpdates({$push: {messages: req.body}})) 
    .then(respondWithResult(res)) 
    .catch(handleError(res)); 
} 

JSON對象我從郵差送:

{ 
    "messages": 
    { 
     "userid": "56d7967745ab81322a964927", 
     "message": "This is a meesage" 
    } 
} 

OR

{ 
    "userid": "56d7967745ab81322a964927", 
    "message": "This is a meesage" 
} 

我可以,如果我有對象ID聊天文件本身,而是我的應用程序內更新對象,我沒有直接引用。我也試過其他幾種方法,但每次我的應用程序返回500錯誤。

您的幫助將不勝感激。

編輯1:這裏是我正在使用的角度全堆棧插件生成的幫助功能。

function respondWithResult(res, statusCode) { 
    statusCode = statusCode || 200; 
    return function(entity) { 
    if (entity) { 
     res.status(statusCode).json(entity); 
    } 
    }; 
} 

function saveUpdates(updates) { 
    return function(entity) { 
    var updated = _.merge(entity, updates); 
    return updated.saveAsync() 
     .spread(updated => { 
     return updated; 
     }); 
    }; 
} 

function removeEntity(res) { 
    return function(entity) { 
    if (entity) { 
     return entity.removeAsync() 
     .then(() => { 
      res.status(204).end(); 
     }); 
    } 
    }; 
} 

function handleEntityNotFound(res) { 
    return function(entity) { 
    if (!entity) { 
     res.status(404).end(); 
     return null; 
    } 
    return entity; 
    }; 
} 

function handleError(res, statusCode) { 
    statusCode = statusCode || 500; 
    return function(err) { 
    res.status(statusCode).send(err); 
    }; 
} 

編輯2:正如我在評論中提到的,問題是與_.Merge功能,沒有合併對象的權利,但它應該是能夠更新的對象。

所以我寫了我自己的功能saveUpdates如下:

function saveUpdatesForNewChat(updates) { 
    return function(entity) { 

    var temp = entity; 
    temp[0].messages.push(updates); 

    console.log('\ntemp:'); 
    console.log(require('util').inspect(temp, { depth: null })); 
    console.log('\nend of ops\n\n'); 

    var updated = _.merge(entity, temp); 
    console.log('out of merge'); 
    console.log(require('util').inspect(updated, { depth: null })); 
    return updated.saveAsync() 
     .spread(updated => { 
     return updated; 
     }); 
    }; 
} 

行,所以我已經離開了控制檯日誌裏面,它是完美的對象保存到數據庫中,但服務器仍然返回500錯誤上更新。

+0

有東西在裏面發臭。你傳遞給'then'和'catch'的函數在你傳遞它們時被調用。你應該傳遞一個函數的定義(只是handleEntityNotFound或handleError或東西,沒有參數,沒有括號)。 – lascort

+0

我想我明白你在說什麼。這是我第一次使用mean-stack,但他們正在爲我的項目使用不同的其他對象。 此外'showByProject'函數按預期工作。 –

+0

更新:嘗試'toJson'或'toObject',它們不起作用。還嘗試操縱這裏提供的代碼:http://mongoosejs.com/docs/documents.html。我現在確定問題出在'_.merge'的'saveUpdates'函數中。我創建了一個新函數並嘗試使用聊天'_id'進行更新,最大它將第二級文檔中的所有消息替換爲第一條消息。 6個小時,我仍然不知道我在做什麼。 –

回答

0

好的!所以我自己找到了答案。

問題是返回的對象是結果集,我在整個結果集上調用save。我從返回的結果集中提取第一個元素,將新消息推送到元素並調用save並開始工作。

下面是代碼:

function saveUpdatesForNewChat(updates) { 
    return function(entity) { 
    var temp = entity[0]; 
    temp.messages.push(updates); 
    var updated = temp; 
    return updated.saveAsync() 
     .spread(updated => { 
     return updated; 
     }); 
    }; 
}