When a student sends a message to a teacher, the message gets saved in both the 'teachers' and 'students' collections. Similarly, when a teacher sends a message to a student, it also gets saved in both the 'teachers' and 'students' collections. I am looking for guidance on how to implement a mongoose transaction in the code provided below to ensure that the message is successfully saved in both collections without any errors. I want to avoid a scenario where the message is only saved in one collection due to an error. Any help or hints would be greatly appreciated.
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
// Connect to Database
module.exports.db = mongoose
.createConnection(process.env.DATABASE, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
useCreateIndex: true
}
);
// Controller
const db = require('./connection');
const session = db.startSession();
session.startTransaction();
module.exports.sendMessage = (req, res) => {
let {sender, receiver, msg, role} = req.body;
var hex = /[0-9A-Fa-f]{6}/g;
sender = (hex.test(sender))? mongoose.Types.ObjectId(sender) : sender;
receiver = (hex.test(receiver))? mongoose.Types.ObjectId(receiver) : receiver;
console.log(sender, receiver, msg, 'send');
if(role === 'tutor') {
let teacherMessage = new TeacherMessageSchema.TeacherMessageSchema({
contentInfo : {
msg : msg
},
sender : sender
})
let studentMessage = new TeacherMessageSchema.TeacherMessageSchema({
contentInfo : {
msg : msg
},
receiver : receiver
})
db.db.collection('teachers').findOneAndUpdate(
{ _id : receiver },
{ $push: { messages: teacherMessage } },
(err, updated) => {
console.log(updated, 'vvv');
updated.value.hashed_password = undefined;
updated.value.salt = undefined;
if(err) {
res.status(404).json(err);
}
if (updated) {
res.status(200).json(updated.value);
db.db.collection('students').findOneAndUpdate(
{ _id : sender },
{ $push: { messages: studentMessage } },
);
}
}, session(session)
)
}
if(role === 'student') {
let studentMessage = new StudentMessageSchema.StudentMessageSchema({
contentInfo : {
msg : msg
},
receiver : receiver
})
let teacherMessage = new TeacherMessageSchema.TeacherMessageSchema({
contentInfo : {
msg : msg
},
sender : sender
})
db.db.collection('students').findOneAndUpdate(
{ _id : sender },
{ $push: { messages: studentMessage } },
(err, updated) => {
console.log(updated, 'sss');
updated.value.hashed_password = undefined;
updated.value.salt = undefined;
if(err) {
res.status(404).json(err);
}
if (updated) {
res.json(updated.value);
db.db.collection('teachers').findOneAndUpdate(
{ _id : receiver },
{ $push: { messages: teacherMessage } },
)
}
},
), session(session)
}
}
Logged variable db
{
db: NativeConnection {
base: Mongoose {
connections: [Array],
models: [Object],
modelSchemas: [Object],
options: [Object],
_pluralize: [Function: pluralize],
plugins: [Array]
},
collections: {},
models: {},
config: { autoIndex: true },
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
useCreateIndex: true,
promiseLibrary: [Function: Promise]
},
client: MongoClient {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
s: [Object],
topology: [ReplSet],
[Symbol(kCapture)]: false
},
name: null,
'$initialConnection': Promise { [Circular] },
db: Db {
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
[Symbol(kCapture)]: false
}
}
}