Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "leads": [],
- "members": [
- {"_id":"someString1","firstName":"...", "lastName":"..."},
- {"_id":"someString2","firstName":"...", "lastName":"..."},
- ],
- "date": "2018-12-12T15:24:45.877Z",
- "_id": "5c11283d7d13687e60c186b3",
- "country": "5c11283d7d13687e60c185d6",
- "city": "Buckridgestad",
- "twitterURL": "qui",
- "bannerPic": "http://lorempixel.com/640/480/people",
- "__v": 0
- }
- ]
- [
- {
- "leads": [],
- "members": [],
- "date": "2018-12-12T15:24:45.877Z",
- "_id": "5c11283d7d13687e60c186b3",
- "country": "5c11283d7d13687e60c185d6",
- "city": "Buckridgestad",
- "twitterURL": "qui",
- "bannerPic": "http://lorempixel.com/640/480/people",
- "__v": 0
- }
- ]
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- // Create Schema
- const ChapterSchema = new Schema({
- country: {
- type: Schema.Types.ObjectId,
- ref: "countries"
- },
- city: {
- type: String,
- required: true
- },
- leads: [
- {
- type: Schema.Types.ObjectId,
- ref: "users"
- }
- ],
- members: [
- {
- type: Schema.Types.ObjectId,
- ref: "users"
- }
- ],
- twitterURL: {
- type: String,
- required: true
- },
- bannerPic: {
- type: String,
- required: true
- },
- date: {
- type: Date,
- default: Date.now()
- }
- });
- module.exports = Chapter = mongoose.model("chapters", ChapterSchema);
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- // Create Schema
- const UserSchema = new Schema({
- username: {
- type: String,
- required: true
- },
- firstName: {
- type: String,
- required: true
- },
- lastName: {
- type: String,
- required: true
- },
- organisation: {
- type: String,
- required: true
- },
- chapter: {
- type: Schema.Types.ObjectId,
- ref: "chapters"
- },
- email: {
- type: String,
- required: true
- },
- admin: {
- type: Boolean,
- default: false
- },
- lead: {
- type: Boolean,
- default: false
- },
- password: {
- type: String,
- required: true
- },
- date: {
- type: Date,
- default: Date.now()
- }
- });
- module.exports = User = mongoose.model("users", UserSchema);
- // @route GET api/chapters
- // @desc Get all Chapters
- // @access Public
- router.get("/", (req, res) => {
- Chapter.find()
- .populate("members")
- .then(chapters => {
- return res.json(chapters);
- })
- .catch(err =>
- res.status(404).json({ nochaptersfound: "No Chapters found" })
- );
- });
- // @route GET api/users
- // @desc Return all users
- // @access Public
- router.get("/", (req, res) => {
- User.find()
- .populate("chapter")
- .exec()
- .then(users => res.status(200).json(users))
- .catch(err => console.log(err));
- User.create({..., chapter: id})...
- const user = await User.create({..., chapter: id})
- const chapter = await Chapter.findOne({ _id: id })
- chapter.members.push(user)
- chapter.save()
- var userPromise = User.create({..., chapter: id}).exec()
- var chapterPromise = Chapter.findOne({ _id: id }).exec()
- Promise.all([userPromise, chapterPromise]).then((user, chapter) => {
- chapter.members.push(user)
- return chapter.save()
- }).then(chapter => {
- // send response
- })
Add Comment
Please, Sign In to add comment