-1
我需要幫助,在客戶端上顯示的對象屬性從MongoDB的對象發送到客戶端的socket.io
服務器
socket.on('question', (data) => {
Question.count().exec((err, count) => {
var random = Math.floor(Math.random() * count);
Question.findOne().skip(random).exec(
function(err, data) {
socket.emit('data', {
question: data.question,
correctAnswer: data.correct_answer,
incorrectAnswer1: data.incorrect_answers[0],
incorrectAnswer2: data.incorrect_answers[1],
incorrectAnswer3: data.incorrect_answers[2]
});
});
});
});
客戶
socket.on('data', function(data) {
if (data) {
$('#questions').html('');
$('#questions').append('<li>' + question + '</li>')
}
});
$('#answer').on('click', function(e){
e.preventDefault();
socket.emit('question', {'data':$(this).serializeArray()});
});
我只得到顯示這
我想在下面顯示問題和答案
這增加了不確定,控制檯日誌對象。 – lejhbah
在你的客戶端做一個console.log。 – sheplu
好的tnx這個工程。它將數據存儲在另一個數據對象中,我不知道爲什麼。 @sheplu – lejhbah