Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. {
  2. username: "Lorem Ipsum",
  3. password: "Dolar Sit",
  4. company: "5c73afcf9a3bde1a40da5184"
  5. }
  6.  
  7. {
  8. username: "Lorem Ipsum",
  9. password: "Dolar Sit",
  10. company: {
  11. company_name: "Blah Blah",
  12. company_address: "Blah Blah Blah",
  13. company_website: "BlahBlah.com",
  14. }
  15. }
  16.  
  17. const Company = require('./company');
  18. Schema = mongoose.Schema
  19.  
  20. // User Schema
  21. const userSchema = mongoose.Schema({
  22. username:{
  23. type: String,
  24. required: true
  25. },
  26. password:{
  27. type: String,
  28. required: true
  29. },
  30. company:{
  31. type: [{ type: Schema.Types.ObjectId, ref: 'Company' }],
  32. required: true
  33. },
  34. created_on:{
  35. type: Date,
  36. default: Date.now
  37. },
  38. updated_on:{
  39. type: Date,
  40. default: Date.now
  41. }
  42. });
  43.  
  44. const User = module.exports = mongoose.model('Users', userSchema);
  45. // Add User
  46. module.exports.addUser = (user, callback) => {
  47. User.create(user, callback);
  48. }
  49.  
  50. const mongoose = require('mongoose');
  51.  
  52. // Book Schema
  53. const companySchema = mongoose.Schema({
  54. company_name:{
  55. type: String
  56. },
  57. target_address:{
  58. type: String
  59. },
  60. target_website:{
  61. type: String
  62. },
  63. created_on:{
  64. type: Date,
  65. default: Date.now
  66. },
  67. updated_on:{
  68. type: Date,
  69. default: Date.now
  70. }
  71. });
  72.  
  73. const Company = module.exports = mongoose.model('Company', companySchema);
  74.  
  75. // Add Company
  76. module.exports.addCompany = (comp, callback) => {
  77. Company.create(comp, callback);
  78. }
  79.  
  80. 'Cast to Array failed for value "{ company_name: 'Moshi Moshi', company_address: 'Bengaluru' }" at path "company"',
  81. name: 'CastError',
  82. stringValue:
  83. '"{ company_name: 'Moshi Moshi', company_address: 'Bengaluru' }"',
  84. kind: 'Array',
  85. value: [Object],
  86. path: 'company',
  87. reason: [MongooseError] } },
  88. _message: 'Users validation failed',
  89. name: 'ValidationError' }
  90. [nodemon] restarting due to changes...
  91. [nodemon] starting `node app.js`
  92. E:MoshiMoshiProjectsMaaamamiablackhole-restmodelsusers.js:17
  93. type: [{ type: ObjectId, ref: 'Company' }],
  94. ^
  95.  
  96. ReferenceError: ObjectId is not defined
  97.  
  98. //incorrect
  99. company:{
  100. type: [{ type: Schema.Types.ObjectId, ref: 'Company' }],
  101. required: true
  102. },
  103.  
  104. //correct
  105. company:{
  106. type: Schema.Types.ObjectId,
  107. ref: 'Company',
  108. required: true
  109. },
  110.  
  111. let companyObject=//your company object
  112. let userObject=//your user object
  113.  
  114. let savedCompanyObject= await addCompanyObject.save() //save CompanyObject
  115. userObject.company = savedCompanyObject._id;
  116.  
  117. await userObject.save() //now save user object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement