Guest User

Untitled

a guest
Apr 30th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. var UserSchema = Schema({
  2. firstName: {
  3. type: String,
  4. required: true
  5. },
  6. secondName: String,
  7. lastName: {
  8. type: String,
  9. required: true
  10. },
  11. email: {
  12. type: String,
  13. unique: true,
  14. required: true
  15. },
  16. password: {
  17. type: String,
  18. required: true
  19. },
  20. status: {
  21. type: String,
  22. required: true
  23. },
  24. roles: [{
  25. type: Schema.ObjectId,
  26. ref: 'Role'
  27. }],
  28.  
  29. publications: [{
  30. title: {
  31. type: String,
  32.  
  33. },
  34. description: String,
  35. status: {
  36. type: String,
  37.  
  38. },
  39. createdAt: {
  40. type: Date
  41. },
  42. updatedAt: {
  43. type: Date,
  44. default: Date.now()
  45. },
  46.  
  47. pictures: [{
  48. name: String
  49. }],
  50.  
  51. categories: [{
  52. type: Schema.Types.ObjectId,
  53. refPath: 'Category.subcategories'
  54. }],
  55.  
  56. var CategorySchema = Schema({
  57. name: String,
  58. subcategories: [{
  59. name: String
  60. }]
  61. });
  62.  
  63. categories: [{
  64. type: Schema.Types.ObjectId,
  65. refPath: 'Category.subcategories'
  66. }],
  67.  
  68. "users": [
  69. {
  70. "roles": [],
  71. "publications": [
  72. {
  73. "updatedAt": "2018-04-30T03:23:11.921Z",
  74. "categories": [
  75. "5ae4a8b0a7510e3bd80917db",
  76. "5ae4a8b0a7510e3bd80917da"
  77. ],
  78. ...
  79.  
  80. {
  81. "categories": [
  82. {
  83. "subcategories": [
  84. {
  85. "_id": "5ae4a8b0a7510e3bd80917db",
  86. "name": "Decoracion"
  87. },
  88. {
  89. "_id": "5ae4a8b0a7510e3bd80917da",
  90. "name": "Electrodomésticos"
  91. },
  92. {
  93. "_id": "5ae4a8b0a7510e3bd80917d9",
  94. "name": "Cocina"
  95. },
  96. {
  97. "_id": "5ae4a8b0a7510e3bd80917d8",
  98. "name": "Muebles"
  99. }
  100. ],
  101. "_id": "5ae4a8b0a7510e3bd80917d7",
  102. "name": "Hogar",
  103. "__v": 0
  104. ...
  105.  
  106. User.populate(users, {
  107. path: 'publications.categories'
  108. }, (err, users) => {
  109.  
  110. if (err) {
  111. res.status(500).send({
  112. message: "Error en la peticion"
  113. });
  114. return;
  115. }
  116.  
  117. if (!users) {
  118. res.status(404).send({
  119. message: "User no encontrado"
  120. });
  121. return;
  122. }
  123.  
  124.  
  125. res.status(200).send({
  126. users
  127. });
  128.  
  129. var userSchema = new Schema({
  130. name: String,
  131. connections: [{
  132. kind: String,
  133. item: { type: ObjectId, refPath: 'connections.kind' }
  134. }]
  135. });
  136.  
  137. var organizationSchema = new Schema({ name: String, kind: String });
  138.  
  139. var User = mongoose.model('User', userSchema);
  140. var Organization = mongoose.model('Organization', organizationSchema);
Add Comment
Please, Sign In to add comment