I have two schema in single schema.js file
var mongoose = require('mongoose');
var user = new mongoose.Schema({
name: String,
add: String,
role: String
});
var Organization = new mongoose.Schema({
name: String,
add: String,
name:String
});
module.exports = {
user: user,
Organization: Organization
};
accessing it like
var models = require("../models/schema");
models.user.findOne()
it says findone is not a function
whereas If I use a single class in a file it is working.
I have gone through this link and did export like above
cant get data from database after multiple schema declared (mongoose + express + mongodb
but not working
any idea?
Thanks
With the help of @anthony I figure out the issue
I need to do the below
module.exports = {
user: mongoose.model('user', user),,
Organization: mongoose.model('Organization', Organization)
};
If you exports more than one file than you will have to import with curly braces { schema1 }
var mongoose = require('mongoose');
var user = new mongoose.Schema({
name: String,
add: String,
role: String
});
var organization = new mongoose.Schema({
name: String,
add: String,
name:String
});
const userSchema = mongoose.model('users', user),
const organizationSchema = mongoose.model('organizations', organization)
module.exports = { User: userSchema, Organization: organizationSchema }
and then import
var { User } = require("../models/schema");
var { Organization } = require("../models/schema");
User.findOne()
Organization.findOne()