I am working with four different models: teacher
, student
, teacherMessageSchema
, and studentMessageSchema
. The teacherMessageSchema
is a subdocument within the 'teacher' model under the messages: [teacherMessageSchema]
property, while the studentMessageSchema
is a subdocument within the 'student' model under the messages: [studentMessageSchema]
property. I am trying to figure out how to add an object to the arrays teacherMessageSchema
and studentMessageSchema
. This is what I have attempted so far:
module.exports.sendMessage = (req, res) => {
let {sender, receiver, msg} = 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;
Teacher.findById({_id: receiver}, function(err, member) {
console.log(member, 'member');
member.messages.push({msg});
console.log('messages', member.messages)
member.save(function(err, updated) {
if (err)
res.send(err);
res.json(updated, 'updated');
});
});
}
However, the property messages
cannot be found.
Teacher and Student Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const crypto = require('crypto');
const {studentMessageSchema, teacherMessageSchema} = require('./message');
const userSchema = new Schema({
name: {
type: String,
trim: true,
required: true,
maxLength: 32
},
email: {
type: String,
unique: true,
trim: true,
required: true,
lowercase: true
}
}, {timestamps: true});
const studentSchema = userSchema.clone();
studentSchema.add({
messages : [studentMessageSchema]
});
const teacherSchema = userSchema.clone();
teacherSchema.add({
messages : [teacherMessageSchema]
});
const User = mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);
module.exports = {
User,
Student,
Teacher
}
Message Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const messageSchema = new Schema({
"contentInfo" : {
"viewed" : {type: Boolean, default: false},
"msg" : {type: String, required: true},
"createdAt" : { type : Date, default: Date.now }
}
});
const studentMessageSchema = messageSchema.clone();
studentMessageSchema.add({
"receiver" : {
type: Schema.ObjectId
}
});
const teacherMessageSchema = messageSchema.clone();
teacherMessageSchema.add({
"sender" : {
type: Schema.ObjectId
}
});
module.exports = {
messageSchema,
teacherMessageSchema,
studentMessageSchema
}
Controller Message
const User = require('../models/user');
const Student = require('../models/user');
const Teacher = require('../models/user');
const mongoose = require('mongoose');
module.exports.sendMessage = (req, res) => {
let {sender, receiver, msg} = 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;
Teacher.findById({_id: receiver}, function(err, member) {
console.log(member, 'member');
member.messages.push({msg});
console.log('messages', member.messages)
member.save(function(err, updated) {
if (err)
res.send(err);
res.json(updated, 'updated');
});
});
}