Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- username: "Lorem Ipsum",
- password: "Dolar Sit",
- company: "5c73afcf9a3bde1a40da5184"
- }
- {
- username: "Lorem Ipsum",
- password: "Dolar Sit",
- company: {
- company_name: "Blah Blah",
- company_address: "Blah Blah Blah",
- company_website: "BlahBlah.com",
- }
- }
- const Company = require('./company');
- Schema = mongoose.Schema
- // User Schema
- const userSchema = mongoose.Schema({
- username:{
- type: String,
- required: true
- },
- password:{
- type: String,
- required: true
- },
- company:{
- type: [{ type: Schema.Types.ObjectId, ref: 'Company' }],
- required: true
- },
- created_on:{
- type: Date,
- default: Date.now
- },
- updated_on:{
- type: Date,
- default: Date.now
- }
- });
- const User = module.exports = mongoose.model('Users', userSchema);
- // Add User
- module.exports.addUser = (user, callback) => {
- User.create(user, callback);
- }
- const mongoose = require('mongoose');
- // Book Schema
- const companySchema = mongoose.Schema({
- company_name:{
- type: String
- },
- target_address:{
- type: String
- },
- target_website:{
- type: String
- },
- created_on:{
- type: Date,
- default: Date.now
- },
- updated_on:{
- type: Date,
- default: Date.now
- }
- });
- const Company = module.exports = mongoose.model('Company', companySchema);
- // Add Company
- module.exports.addCompany = (comp, callback) => {
- Company.create(comp, callback);
- }
- 'Cast to Array failed for value "{ company_name: 'Moshi Moshi', company_address: 'Bengaluru' }" at path "company"',
- name: 'CastError',
- stringValue:
- '"{ company_name: 'Moshi Moshi', company_address: 'Bengaluru' }"',
- kind: 'Array',
- value: [Object],
- path: 'company',
- reason: [MongooseError] } },
- _message: 'Users validation failed',
- name: 'ValidationError' }
- [nodemon] restarting due to changes...
- [nodemon] starting `node app.js`
- E:MoshiMoshiProjectsMaaamamiablackhole-restmodelsusers.js:17
- type: [{ type: ObjectId, ref: 'Company' }],
- ^
- ReferenceError: ObjectId is not defined
- //incorrect
- company:{
- type: [{ type: Schema.Types.ObjectId, ref: 'Company' }],
- required: true
- },
- //correct
- company:{
- type: Schema.Types.ObjectId,
- ref: 'Company',
- required: true
- },
- let companyObject=//your company object
- let userObject=//your user object
- let savedCompanyObject= await addCompanyObject.save() //save CompanyObject
- userObject.company = savedCompanyObject._id;
- await userObject.save() //now save user object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement