Guest User

Untitled

a guest
Dec 12th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. [
  2. {
  3. "leads": [],
  4. "members": [
  5. {"_id":"someString1","firstName":"...", "lastName":"..."},
  6. {"_id":"someString2","firstName":"...", "lastName":"..."},
  7. ],
  8. "date": "2018-12-12T15:24:45.877Z",
  9. "_id": "5c11283d7d13687e60c186b3",
  10. "country": "5c11283d7d13687e60c185d6",
  11. "city": "Buckridgestad",
  12. "twitterURL": "qui",
  13. "bannerPic": "http://lorempixel.com/640/480/people",
  14. "__v": 0
  15. }
  16. ]
  17.  
  18. [
  19. {
  20. "leads": [],
  21. "members": [],
  22. "date": "2018-12-12T15:24:45.877Z",
  23. "_id": "5c11283d7d13687e60c186b3",
  24. "country": "5c11283d7d13687e60c185d6",
  25. "city": "Buckridgestad",
  26. "twitterURL": "qui",
  27. "bannerPic": "http://lorempixel.com/640/480/people",
  28. "__v": 0
  29. }
  30. ]
  31.  
  32. const mongoose = require("mongoose");
  33. const Schema = mongoose.Schema;
  34.  
  35. // Create Schema
  36. const ChapterSchema = new Schema({
  37. country: {
  38. type: Schema.Types.ObjectId,
  39. ref: "countries"
  40. },
  41. city: {
  42. type: String,
  43. required: true
  44. },
  45. leads: [
  46. {
  47. type: Schema.Types.ObjectId,
  48. ref: "users"
  49. }
  50. ],
  51. members: [
  52. {
  53. type: Schema.Types.ObjectId,
  54. ref: "users"
  55. }
  56. ],
  57. twitterURL: {
  58. type: String,
  59. required: true
  60. },
  61. bannerPic: {
  62. type: String,
  63. required: true
  64. },
  65. date: {
  66. type: Date,
  67. default: Date.now()
  68. }
  69. });
  70.  
  71. module.exports = Chapter = mongoose.model("chapters", ChapterSchema);
  72.  
  73. const mongoose = require("mongoose");
  74. const Schema = mongoose.Schema;
  75.  
  76. // Create Schema
  77. const UserSchema = new Schema({
  78. username: {
  79. type: String,
  80. required: true
  81. },
  82. firstName: {
  83. type: String,
  84. required: true
  85. },
  86. lastName: {
  87. type: String,
  88. required: true
  89. },
  90. organisation: {
  91. type: String,
  92. required: true
  93. },
  94. chapter: {
  95. type: Schema.Types.ObjectId,
  96. ref: "chapters"
  97. },
  98. email: {
  99. type: String,
  100. required: true
  101. },
  102. admin: {
  103. type: Boolean,
  104. default: false
  105. },
  106. lead: {
  107. type: Boolean,
  108. default: false
  109. },
  110. password: {
  111. type: String,
  112. required: true
  113. },
  114. date: {
  115. type: Date,
  116. default: Date.now()
  117. }
  118. });
  119.  
  120. module.exports = User = mongoose.model("users", UserSchema);
  121.  
  122. // @route GET api/chapters
  123. // @desc Get all Chapters
  124. // @access Public
  125.  
  126. router.get("/", (req, res) => {
  127. Chapter.find()
  128. .populate("members")
  129. .then(chapters => {
  130. return res.json(chapters);
  131. })
  132. .catch(err =>
  133. res.status(404).json({ nochaptersfound: "No Chapters found" })
  134. );
  135. });
  136.  
  137. // @route GET api/users
  138. // @desc Return all users
  139. // @access Public
  140.  
  141. router.get("/", (req, res) => {
  142. User.find()
  143. .populate("chapter")
  144. .exec()
  145. .then(users => res.status(200).json(users))
  146. .catch(err => console.log(err));
  147.  
  148. User.create({..., chapter: id})...
  149.  
  150. const user = await User.create({..., chapter: id})
  151. const chapter = await Chapter.findOne({ _id: id })
  152. chapter.members.push(user)
  153. chapter.save()
  154.  
  155. var userPromise = User.create({..., chapter: id}).exec()
  156. var chapterPromise = Chapter.findOne({ _id: id }).exec()
  157. Promise.all([userPromise, chapterPromise]).then((user, chapter) => {
  158. chapter.members.push(user)
  159. return chapter.save()
  160. }).then(chapter => {
  161. // send response
  162. })
Add Comment
Please, Sign In to add comment