This is my mongoose schema for work:
var mongoose = require('mongoose');
var WorkSchema = mongoose.Schema({
Title:{
type:String,
required:true,
unique:true
},
VideoURL:String,
Image: { data: Buffer, contentType: String },
Price:Number,
Username:String,
Company_id:[{type:mongoose.Schema.Types.ObjectId, ref:'Company'}]
});
var Work = module.exports = mongoose.model("Work", WorkSchema);
And this is the my work controller:
let Work = require('../Models/Work');
let Company = require('../Models/Company');
let WorkController = {
createWork:function(req, res){
let trial = new Work();
console.log( req.session.Username+ " session");
trial.Username= req.session.Username;
trial.VideoURl= req.body.VideoURl;
trial.Price=req.body.Price;
trial.Title= req.body.Title;
User=req.session.Username;
Company.findOne( {Username:User},function(err, company){
trial.Company_id=company._id;
trial.save(function(err, Work){
if(err){
res.send(err.message);
console.log(err);
}
});
});
}
}
module.exports = WorkController;
And this is my schema for Company: let Company = require('../Models/Company');
let CompanyController = {
login:function(req,res){
var Username = req.body.Username;
var Password =req.body.Password;
Company.findOne({Username:Username,Password:Password},function(err,Company)
{
if(!Company){
return console.log('you are not athorized ');
res.status(500).send(errorMsg);
;}
else{
res.json(Company);
req.session.Username = Company.Username;
console.log("username session: " + req.session.Username);
}
})
}
,
createCompany:function(req, res){
let company = new Company(req.body);
company.save(function(err, company){
if(err){
res.send(err.message)
console.log(err);
}
else{
req.session.Username= req.body.Username;
console.log(company);
}
})
}
}
module.exports = CompanyController;
When the the company user logs in and tries to create work, this error appears in the console "TypeError: Cannot read property 'id' of null"
via user7632716
No comments:
Post a Comment