If I made a chat app, how would I save the chats to a database? mongo/replit db preferred.
Could you give me a basic code snippet? Thanks!
const Client = require("@replit/database");
const db = new Client();
function createGroup(name, users) {
db.get(name).then(function(chat) {
if (!chat) {
db.set(name, {
"name": name,
"users": [ "user1", "user2" ],
"messages": [ ]
});
}
});
}
function message(chat, username, message) {
const sendTime = Date.now();
db.get(chatName, function(chat) {
if (chat && chat.users.includes(username)) {
chat.messages.push({
"author": username,
message,
sendTime
});
db.set(chatName, chat);
}
});
}
3 Likes
oh my. I never thought of that. Can I invite you to a repl plz?
1 Like
Sure, happy to help
thx. If it works, I’ll mark as a solution.
very true you know
1 Like
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.