我一直在尝试在我的应用程序中实现聊天模块。我对MERN全栈没有什么经验,这是我第一次使用socket.io。但是,每当我尝试创建聊天室时,它都会引发错误并且不会创建任何聊天室。
我尝试搜索解决方案,但找不到任何解决方法。
(很抱歉,如果有很多代码,我会尽可能地缩短它,但是我不确定哪个部分与问题相关)。
Sub SplitWorkbook()
Dim ws As Worksheet
Dim DisplayStatusBar As Boolean
DisplayStatusBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
Dim NewFileName As String
Application.StatusBar = ThisWorkbook.Sheets.Count & _
" Remaining Sheets"
If ThisWorkbook.Sheets.Count <> 1 Then
NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xlsm" _
'Macro-Enabled
' NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xlsx" _
'Not Macro-Enabled
ws.Copy
ActiveWorkbook.Sheets(1).Name = "Sheet1"
ActiveWorkbook.SaveAs Filename:=NewFileName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
' ActiveWorkbook.SaveAs Filename:=NewFileName, _
FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close SaveChanges:=False
Else
NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xlsm"
' NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xlsx"
ws.Name = "Sheet1"
End If
Next
Application.DisplayAlerts = True
Application.StatusBar = False
Application.DisplayStatusBar = DisplayStatusBar
Application.ScreenUpdating = True
End Sub
我有3个名为“ ChatDetails.js”,“ ChatList.js”和“ CreateRoom.js”的订单文件,如果我必须在此处发布代码,则将其为idk。
这是模型
class Chat extends Component {
constructor(props) {
super(props);
this.state = {
modal: false,
chatLists: []
};
//this.socket = io("localhost:8080");
}
componentDidMount() {
// if (this.props.auth.isAuthenticated) {
// this.props.history.push("/dashboard");
// }
axios.get(`/chats/list`).then(res => {
const chatLists = res.data;
this.setState({ chatLists });
});
}
sendChatId(i) {
return <ChatDetail chatId={i} />;
}
render() {
return (
<div className="Chat">
{/* <AppNavbar /> */}
<CreateRoom />
<hr />
<h3>Chat Room List</h3>
<Router>
<ListGroup>
{this.state.chatLists.map(chatList => (
<ListGroupItem tag="a" key={chatList._id}>
<Link to={`/chatDetail`}>
{chatList.roomTitle}
{this.sendChatId(chatList._id)}
</Link>
<Route path={`/chatDetail`} component={ChatDetail} />
</ListGroupItem>
))}
</ListGroup>
</Router>
</div>
);
}
}
Chat.propTypes = {
auth: PropTypes.object.isRequired
//errors: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth
//errors: state.errors
});
export default connect(mapStateToProps)(withRouter(Chat));
聊天详细信息模型
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Chat Schema
const ChatSchema = new Schema({
roomTitle: {
type: String,
required: true
},
createdBy: {
type: String,
required: true
}
});
module.exports = Chat = mongoose.model("Chat", ChatSchema);
//const Chat = module.exports = mongoose.model('Chat', ChatSchema);
module.exports.addChatRoom = function(newChat, callback) {
newChat.save(callback);
};
聊天路线
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
//const config = require('../config/database');
// Chat Detail Schema
const ChatDetailSchema = new Schema({
chatMsg: {
type: String,
required: true
},
msgBy: {
type: String,
required: true
},
chatId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Chat",
required: true
}
});
module.exports = ChatDetail = mongoose.model("ChatDetail", ChatDetailSchema);
//const ChatDetail = module.exports = mongoose.model('ChatDetail', ChatDetailSchema);
module.exports.addChatMsg = function(newMsg, callback) {
newMsg.save(callback);
};
在应用程序的入口点server.js
const express = require("express");
const router = express.Router();
//const config = require('../../config/database');
const Chat = require("../../models/Chat");
const ChatDetail = require("../../models/ChatDetail");
const http = require("http").Server(express);
const io = require("socket.io")(http);
//const express = require("express");
//const router = express.Router();
const mongoose = require("mongoose");
const passport = require("passport");
// Post model
//const Post = require("../../models/Post");
// Profile model
//const Profile = require("../../models/Profile");
// get list of all chat room list
/**
* @route GET api/chats/list
* @desc Get all chatrooms list
*/
router.get("/list", (req, res, next) => {
Chat.find()
.sort({ date: -1 })
.then(chats => res.json(chats));
});
/**
* @route POST api/chats/create
* @desc Create a new chat room
* @param {String} roomTitle
* @param {String} createdBy
*/
router.post("/create", (req, res) => {
let newChat = new Chat({
roomTitle: req.body.roomTitle,
createdBy: req.body.createdBy
});
Chat.addChatRoom(newChat, (err, chat) => {
if (err) {
res.json({
success: false,
msg: "Can not create Chat room"
});
} else {
res.json({
success: true,
msg: "Successfully created a chat room"
});
}
});
});
/**
* @route GET api/chats/detail/:id
* @desc Get Chat Details
* @param {String} chatId
*/
router.get("/detail/:id", (req, res, next) => {
const chatId = req.params.id;
Chat.findById(chatId)
.then(function(chat) {
if (chat) {
const queryForMsgs = ChatDetail.find();
queryForMsgs.where("chatId", chatId);
queryForMsgs.populate("chatId");
queryForMsgs.exec(function(err, result) {
if (err) {
res.json("No chat msgs here" + err);
} else {
res.json(result);
}
});
}
})
.catch(err => res.status(404).json({ success: false }));
});
/**
* @route POST api/chats/addMsg/:id
* @desc Add new chat msg with chatRoom Id, username, message
* @param {String} chatId
*/
router.post("/addMsg/:id", (req, res, next) => {
const chatId = req.params.id;
let newMsg = new ChatDetail({
chatId: chatId,
chatMsg: req.body.chatMsg,
msgBy: req.body.msgBy
});
ChatDetail.addChatMsg(newMsg, (err, chatMsgs) => {
if (err) {
res.json({
success: false,
msg: "No msg send"
});
} else {
// res.json ({success: true, msg: 'Successfully Send a msg'});
io.on("connection", function(socket) {
console.log("A New msg send....");
socket.on("getMsgBy", function(data) {
console.log(data);
socket.emit("msgData", { msgBy: data });
});
socket.on("msgToAll", function(data) {
//Send message to everyone
io.sockets.emit("newmsg", data);
});
});
}
});
});
/**
* Delete chat msg from chat detail
* @route DELETE api/chats/delete/:id
* @desc Delete A chat message
* @param {String} chatMsgId
* @return {Boolean}
*/
router.delete("/delete/:id", (req, res) => {
const chatMsgId = req.params.id;
ChatDetail.findById(chatMsgId)
.then(chat => chat.remove().then(() => res.json({ success: true })))
.catch(err => res.status(404).json({ success: false }));
});
/**
* @route POST api/chats/update/:id
* @desc Update chat Messages
* @param {String} chatMsgId
*/
router.post("update/:id", (req, res) => {
const chatMsgId = req.params.id;
ChatDetail.findById(chatMsgId).exec(function(err, result) {
result.set({
chatMsg: req.body.chatMsg,
msgBy: req.body.msgBy
});
result.save(function(err, newResult) {
if (err) {
console.log(err);
} else {
io.on("connection", function(socket) {
console.log("Msg updates....");
socket.on("getMsgBy", function(data) {
console.log(data);
socket.emit("msgData", { msgBy: data });
});
socket.on("msgToAll", function(data) {
//Send message to everyone
io.sockets.emit("newmsg", data);
});
});
}
});
});
});
// test routes
router.get("/test", (req, res, next) => {
res.send("This route works fine");
});
module.exports = router;
我希望用户输入聊天室名称和他的名字才能进入前端。