我无法在客户端显示数据。数据正存储在mongodb集合中,但是我无法在客户端发送数据。我想在客户端上的两个用户之间发送对话数据,但是在控制台中却得到了空数组,为什么呢? server.js文件中的.find()
错误吗?我添加了server.js和chat.js(要在其中显示数据的客户端文件)
server.js:
io.on("connection", function(socket){
console.log("Socket Connection Established with ID :"+ socket.id)
socket.on('disconnect', function(){
console.log('User Disconnected');
});
let chat = db.collection('chat');
let conversation = db.collection('conversation');
let user1,user2;
socket.on('GET_USER', function (data) {
console.log(data);
user2 = data.user2;
});
socket.on('LOGGEDIN_USER', function(data) {
console.log(data);
user1 = data.user1;
});
socket.on('SEND_MESSAGE', function(data){
let author = data.author;
let message = data.message;
let date = data.date;
// Check for name and message
if(author !== '' || message !== '' || date !== ''){
// Insert message
chat.insert({author:author, message: message, date:date}, function(){
io.emit('RECEIVE_MESSAGE', [data]);
});
conversation.findOne({ $and :[{user1: user1}, {user2: user2}] }, function (err, existingConversation){
if(existingConversation === null){
conversation.insert({user1: user1, user2: user2, conversation: [{author: author, message: message, date:date}] })
}else{
conversation.update({user1: user1, user2: user2},
{$push: {conversation : { author : author ,message : message,date : date }} })
}
})
}
});
conversation.find({ $and : [ {user1: user1}, {user2: user2} ] }).sort({_id: 1}).toArray(function (err, res) {
if(err){
throw err;
}
// Emit the messages
io.emit('RECEIVE_MESSAGE', res);
})
})
chat.js:
componentDidMount() {
this.props.fetchUsers();
this.socket.on('RECEIVE_MESSAGE', data => {
console.log(data); <--- This shows empty array in browser console
this.addMessage(data);
});
}
sendMessage(event) {
event.preventDefault();
if(this.state.message !== ''){
this.socket.emit('SEND_MESSAGE', {
author: this.props.match.params.user,
message: this.state.message,
date: Date.now()
});
this.socket.emit('LOGGEDIN_USER', { user1: this.props.match.params.user});
}
};
addMessage(data) {
this.props.saveAuthor(this.props.match.params.user)
this.props.saveMessages(data)
this.setState({
message: ''
});
};
handleClick = (userName) => {
this.socket.emit('GET_USER', { user2: userName });
}
存储在数据库中的样本数据:
{
"_id": {
"$oid": "5be039cf2b04ba5594939d48"
},
"user1": "Aditya",
"user2": "vineet",
"conversation": [
{
"author": "Aditya",
"message": "Hello from Aditya",
"date": 1541421517633
},
{
"author": "Aditya",
"message": "This is second message from Aditya",
"date": 1541421556047
}
]
}
屏幕截图:
答案 0 :(得分:2)
嗯,您在conversation.find({ user1: user1 })
事件的同一时间调用connection
,那么user1
现在是undefined
。您可以将console.log(user1)
放在查询上方以检查user1值:
console.log("Before find user query: ", user1)
conversation.find({ user1: user1 })....
输出日志=> Before find user query: undefined
,这是您发送给客户端的数据为空数组的原因。
修补程序:仅在真正获得user1
socket.on('LOGGEDIN_USER', function (data) {
console.log(data);
user1 = data.user1;
conversation.find({ user1: user1 }).sort({ _id: 1 }).toArray(function (err, res) {
if (err) {
throw err;
}
// Emit the messages
io.emit('RECEIVE_MESSAGE', res);
})
});