0

EDIT: My code here is correct, as is the answer I received. I realized after posting and getting an answer that the error was coming from another file not posted here. I'm posting this edit because I don't want to delete.

I'm quite sure I'm importing a schema, but for some reason it's being interpreted as a Model and I'm getting this Invalid Schema Configuration error. Here's the schema that's getting imported:

user.js:

const mongoose = require('mongoose');
let Schema = mongoose.Schema;

const userSchema = new Schema({
    name: { type: String, required: true },
    email: { type: String, required: true },
    //eventsUpdated: [Schema.ObjectId],   // ids of events updated
    //flags: [Schema.ObjectId],   // ids of flags of events
});

const user = mongoose.model('user', userSchema);

module.exports = user;

And here's the file that's importing: flag.js:

const mongoose = require('mongoose');
let Schema = mongoose.Schema;
const userSchema = require('./user').schema;

const flagSchema = new Schema({
    field: String,
    count: Number,
    IPs: [String],
    lastCleared: Date,
    clearedBy: [userSchema]
});

const flag = mongoose.model('flag', flagSchema);

module.exports = flag;

I've tried importing different ways, like explicitly exporting only the userSchema in user.js, and nothing has solved it or even given a different error. I've tried solutions offered by similar questions like this one. Please help, and thank you!

YoungZaphod
  • 472
  • 1
  • 5
  • 15

1 Answers1

0

you are not exporting userSchema

instead of

module.exports = user;

use

module.exports = {
  user,
  userSchema
};

and then require it

const { userSchema } = require('./user');
MRsabs
  • 85
  • 3
  • But I am importing the schema with this line, correct? ```const userSchema = require('./user').schema;``` Also, I tried your suggestion, it's giving me the same error. – YoungZaphod Sep 05 '20 at 00:40
  • yes you are correct. and this is an example you can check it out maybe it would help https://repl.it/repls/OffshoreCluelessCommand – MRsabs Sep 05 '20 at 01:07