Reputation: 3543
I have a account model like this and it works fine:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const card = new Schema(
{
id: String,
performance: [Number],
type: String,
reference: [String],
hardPronounces: [String],
words: [String],
translate: String,
colorize: [String],
image: String,
due: Number,
interact: {
recordTimes: [{ start: Number, end: Number }],
recordingStorage: { x: [Number], y: [Number] },
initialPositions: { left: String, top: String }
}
}
);
const schema = new Schema({
email: { type: String, unique: true, required: true },
passwordHash: { type: String, required: true },
title: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
acceptTerms: Boolean,
role: { type: String, required: true },
verificationToken: String,
verified: Date,
resetToken: {
token: String,
expires: Date
},
passwordReset: Date,
created: { type: Date, default: Date.now },
updated: Date,
userLevel: { type: String, default: 'starter' },
cards: {
starter : [card],
intermediate : [card],
advanced : [card],
}
});
schema.virtual('isVerified').get(function () {
return !!(this.verified || this.passwordReset);
});
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
// remove these props when object is serialized
delete ret._id;
delete ret.passwordHash;
}
});
module.exports = mongoose.model('Account', schema);
Then I have the helper function which allows me to use the above model like :
const account = await db.Account.findOne({ email });
Here is the helper function:
const config = require('config.json');
const mongoose = require('mongoose');
const connectionOptions = { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false };
mongoose.connect(process.env.MONGODB_URI || config.connectionString, connectionOptions);
mongoose.Promise = global.Promise;
module.exports = {
Mother: require('accounts/mother.model'),
Account: require('accounts/account.model'),
RefreshToken: require('accounts/refresh-token.model'),
isValidId
};
function isValidId(id) {
return mongoose.Types.ObjectId.isValid(id);
}
The issue is if I create another model named Mother
I cannot see the new model in the database:
Here is the Mother model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const card = new Schema(
{
id: String,
performance: [Number],
type: String,
reference: [String],
hardPronounces: [String],
words: [String],
translate: String,
colorize: [String],
image: String,
due: Number,
interact: {
recordTimes: [{ start: Number, end: Number }],
recordingStorage: { x: [Number], y: [Number] },
initialPositions: { left: String, top: String }
}
}
);
const mother = new Schema({
cards: {
starter : [card],
intermediate : [card],
advanced : [card]
}
});
module.exports = mongoose.model('Mother', mother);
And here is what I see in mangoDB:
I don't know how to debug this please help..
EDIT: here is the overview of what I have:
EDIT2: Here is the saving function:
async function saveMotherCard(card, level) {
// card is based on the schema
// level is 'starter'
const db = new db.Mother(card[level]);
db.cards[level].push(card);
const a = await db.save();
console.log(a);
}
EDIT 3:
I used logs to show you that your code as mine wont go down the lines so with this code I can see only first console log 1:
console.log('1')
const db = new db.Mother(); // No need to specify the card here when pushing it
console.log('2')
db.cards[level].push(card);
console.log('3')
const a = await db.save();
console.log(a);
EDIT 4: my register function works fine, I thought maybe this helps:
async function register(params, origin) {
// validate
if (await db.Account.findOne({ email: params.email })) {
// send already registered error in email to prevent account enumeration
return await sendAlreadyRegisteredEmail(params.email, origin);
}
// create account object
const account = new db.Account(params);
// first registered account is an admin
const isFirstAccount = (await db.Account.countDocuments({})) === 0;
account.role = isFirstAccount ? Role.Admin : Role.User;
account.verificationToken = randomTokenString();
// hash password
account.passwordHash = hash(params.password);
// save account
await account.save();
// send email
await sendVerificationEmail(account, origin);
}
Upvotes: 0
Views: 239
Reputation: 721
Since const db = await db.Mother.find();
results in an empty array, you will have to make an entry for Mother Model in order to make Mother collection appear in the DB.
Also to make your code cleaner, since you have the same card schema being used in Account & Mother, it would be great if you put it in a seperate file.
Edit 1:
const db = new db.Mother(); // No need to specify the card here when pushing it
db.cards[level].push(card);
const a = await db.save();
console.log(a);
Edit 2 & 3:
console.log('1')
const newMother = new db.Mother({}); // const name already assigned
console.log('2', newMother)
newMother.cards[level].push(card);
console.log('3', newMother)
const a = await newMother.save();
console.log(a);
Upvotes: 1