Deleting from two different schemas












1














I created a Gym Web App for experience and I have come to a stage where I can not figure out how to solve this. I have Users and GymClasses.



When you book yourself into a gym class, it saves the userId in the classMembers array. Then also stores the class attached to the user in their schema.



So the issue is when trying to delete a class which has users signed up, it deletes the class as expected but I need to get every userId which is signed up into that class and delete the classId of their attending classes.



This is the images of the class Schemas:



enter image description here



As you can see the Class Schema has a classMembers array which is populated by userId's which are signed up to the class.



This is the user Schema:



const bookedClassesSchema = mongoose.Schema({
classId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'createClass',
},
dateBooked: {
type: Date,
}
})

const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
address: {
type: String,
required: true
},
gymLocation: {
type: String,
required: true
},
bookedClasses: [bookedClassesSchema]
});


Now from thought on this would be to before deleting the class, loop through the classMembers array grab every userId go to the user and since we already have the ClassId which we are deleting then delete that ClassId from the bookedClasses array. I am not sure on how I can do this so any advice would be great!



This is what I have done so far:



exports.deleteClass = (req, res) => {
let classId = req.params.id;
let users = ;
GymClass.findById({
_id: classId
}, 'classMembers', (res ,err) => {
if (err) {
res.status(401).json({
message: "Error Occured!"
})
} else {
res.push(users);
users.forEach(function(element) {
console.log(element);
});
users.forEach( (users.userId) => {
console.log(users.userId);
});


I have not gone much further than this, any examples or help would be super appreciated!










share|improve this question



























    1














    I created a Gym Web App for experience and I have come to a stage where I can not figure out how to solve this. I have Users and GymClasses.



    When you book yourself into a gym class, it saves the userId in the classMembers array. Then also stores the class attached to the user in their schema.



    So the issue is when trying to delete a class which has users signed up, it deletes the class as expected but I need to get every userId which is signed up into that class and delete the classId of their attending classes.



    This is the images of the class Schemas:



    enter image description here



    As you can see the Class Schema has a classMembers array which is populated by userId's which are signed up to the class.



    This is the user Schema:



    const bookedClassesSchema = mongoose.Schema({
    classId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'createClass',
    },
    dateBooked: {
    type: Date,
    }
    })

    const userSchema = mongoose.Schema({
    name: {
    type: String,
    required: true
    },
    email: {
    type: String,
    required: true,
    unique: true
    },
    password: {
    type: String,
    required: true
    },
    address: {
    type: String,
    required: true
    },
    gymLocation: {
    type: String,
    required: true
    },
    bookedClasses: [bookedClassesSchema]
    });


    Now from thought on this would be to before deleting the class, loop through the classMembers array grab every userId go to the user and since we already have the ClassId which we are deleting then delete that ClassId from the bookedClasses array. I am not sure on how I can do this so any advice would be great!



    This is what I have done so far:



    exports.deleteClass = (req, res) => {
    let classId = req.params.id;
    let users = ;
    GymClass.findById({
    _id: classId
    }, 'classMembers', (res ,err) => {
    if (err) {
    res.status(401).json({
    message: "Error Occured!"
    })
    } else {
    res.push(users);
    users.forEach(function(element) {
    console.log(element);
    });
    users.forEach( (users.userId) => {
    console.log(users.userId);
    });


    I have not gone much further than this, any examples or help would be super appreciated!










    share|improve this question

























      1












      1








      1


      1





      I created a Gym Web App for experience and I have come to a stage where I can not figure out how to solve this. I have Users and GymClasses.



      When you book yourself into a gym class, it saves the userId in the classMembers array. Then also stores the class attached to the user in their schema.



      So the issue is when trying to delete a class which has users signed up, it deletes the class as expected but I need to get every userId which is signed up into that class and delete the classId of their attending classes.



      This is the images of the class Schemas:



      enter image description here



      As you can see the Class Schema has a classMembers array which is populated by userId's which are signed up to the class.



      This is the user Schema:



      const bookedClassesSchema = mongoose.Schema({
      classId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'createClass',
      },
      dateBooked: {
      type: Date,
      }
      })

      const userSchema = mongoose.Schema({
      name: {
      type: String,
      required: true
      },
      email: {
      type: String,
      required: true,
      unique: true
      },
      password: {
      type: String,
      required: true
      },
      address: {
      type: String,
      required: true
      },
      gymLocation: {
      type: String,
      required: true
      },
      bookedClasses: [bookedClassesSchema]
      });


      Now from thought on this would be to before deleting the class, loop through the classMembers array grab every userId go to the user and since we already have the ClassId which we are deleting then delete that ClassId from the bookedClasses array. I am not sure on how I can do this so any advice would be great!



      This is what I have done so far:



      exports.deleteClass = (req, res) => {
      let classId = req.params.id;
      let users = ;
      GymClass.findById({
      _id: classId
      }, 'classMembers', (res ,err) => {
      if (err) {
      res.status(401).json({
      message: "Error Occured!"
      })
      } else {
      res.push(users);
      users.forEach(function(element) {
      console.log(element);
      });
      users.forEach( (users.userId) => {
      console.log(users.userId);
      });


      I have not gone much further than this, any examples or help would be super appreciated!










      share|improve this question













      I created a Gym Web App for experience and I have come to a stage where I can not figure out how to solve this. I have Users and GymClasses.



      When you book yourself into a gym class, it saves the userId in the classMembers array. Then also stores the class attached to the user in their schema.



      So the issue is when trying to delete a class which has users signed up, it deletes the class as expected but I need to get every userId which is signed up into that class and delete the classId of their attending classes.



      This is the images of the class Schemas:



      enter image description here



      As you can see the Class Schema has a classMembers array which is populated by userId's which are signed up to the class.



      This is the user Schema:



      const bookedClassesSchema = mongoose.Schema({
      classId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'createClass',
      },
      dateBooked: {
      type: Date,
      }
      })

      const userSchema = mongoose.Schema({
      name: {
      type: String,
      required: true
      },
      email: {
      type: String,
      required: true,
      unique: true
      },
      password: {
      type: String,
      required: true
      },
      address: {
      type: String,
      required: true
      },
      gymLocation: {
      type: String,
      required: true
      },
      bookedClasses: [bookedClassesSchema]
      });


      Now from thought on this would be to before deleting the class, loop through the classMembers array grab every userId go to the user and since we already have the ClassId which we are deleting then delete that ClassId from the bookedClasses array. I am not sure on how I can do this so any advice would be great!



      This is what I have done so far:



      exports.deleteClass = (req, res) => {
      let classId = req.params.id;
      let users = ;
      GymClass.findById({
      _id: classId
      }, 'classMembers', (res ,err) => {
      if (err) {
      res.status(401).json({
      message: "Error Occured!"
      })
      } else {
      res.push(users);
      users.forEach(function(element) {
      console.log(element);
      });
      users.forEach( (users.userId) => {
      console.log(users.userId);
      });


      I have not gone much further than this, any examples or help would be super appreciated!







      javascript mongodb express mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 13:32









      Andrew

      18411




      18411
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375736%2fdeleting-from-two-different-schemas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375736%2fdeleting-from-two-different-schemas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          'app-layout' is not a known element: how to share Component with different Modules

          WPF add header to Image with URL pettitions [duplicate]