macha devendher macha devendher - 4 months ago 34
Node.js Question

throw new MongooseError.MissingSchemaError(name); MissingSchemaError: Schema hasn't been registered for model "usermodel"

I am getting Error "throw new MongooseError.MissingSchemaError(name); MissingSchemaError: Schema hasn't been registered for model "usersmodel"."

Here userregistration.js

var mongoose=require("mongoose");
var Schema=mongoose.Schema;

var users= new Schema({
firstname:String,
secondname:String,
apartmentname:String,
block:String,
floor:String,
flotnumber:String,
residencytype:String,
ownername: { type: String, default:"" },
password:{type:String,default:"password"},
email:String,
contact:{type:Number,default:0},
address:String,
role:{type:String,default:"user"},
approvalstatus:{type:String,default:false}

});

module.exports=mongoose.model("users",users);


Maintaincemodel.js

var mongoose=require('mongoose');
var Schema=mongoose.Schema;

var MaintenanceSchema= new Schema({
name: [{ type: Schema.ObjectId, ref: 'usersmodel' }],
Flat:{type:String},
Squareft:{type:String},
Month:{type:Number},
Year:{type:Number},
Amount:{type:Number},
Paid:{type:String},
Date:{type:Date,default:Date.now}
});

module.exports=mongoose.model('Maintenance',MaintenanceSchema);


Server.js

var mongoose = require('mongoose');
var Schema=mongoose.Schema;

var maintenancemodel=require('./public/models/maintenance.js');
var usersmodel=require('./public/models/userregistration.js');

app.get("/maintenance",function(req,res){

maintenancemodel.find({})
.populate('name')
.exec(function(err,maintenances){
if(err) console.log("error occures");
else{
console.log("the person name is",maintenances.name[0].firstname);
res.json(maintenances);
}
})

});


I am successfully able to save the data into database But getting error when i was trying to populate data.

I have gone through multiple answers of same questions but i didn't find anything correct solutions for mine.

raj raj
Answer

in MaintenanceSchema change name: [{ type: Schema.ObjectId, ref: 'usersmodel' }], to name: [{ type: Schema.ObjectId, ref: 'users' }],

Comments