title: Deleting an element from an Object in MongoDB with Mongoose
body: <p>I am trying to remove a <code>card</code> from the user's current project.
  Each card follows the <code>CardSchema</code>.</p>
  <p>I'm building a kanban board - this might be useful to know.</p>
  <p>However, I've been unable to delete a card.</p>
  <p>The structure of my database for this is as follows:</p>
  <pre class="lang-js prettyprint-override"><code>&quot;cards&quot;: [
      {
          &quot;pending&quot;: {
              &quot;cardIDHere&quot;: {
                  // stuff here
              }
          },
          &quot;inprogress&quot;: {},
          &quot;completed&quot;: {}
      }
  ]
  
  // or
  
  project
      | - cards [Array]
          | - 0 [Object]
              | - pending [Object]
                  | - cardIDHere [Object]
              | - inprogress [Object]
              | - completed [Object]
     
  </code></pre>
  <p>In other words, to get to a card you need:
  <code>cards.0.&lt;panel-name&gt;.&lt;card-id&gt;</code>.</p>
  <p><code>panel-name</code> is the parent of the card: <code>pending</code>, etc.</p>
  <p>My code:</p>
  <pre class="lang-js prettyprint-override"><code>exports.updateCardPlace = async(req, res, next) =&gt; {
      const parent = await Card.findOne({&quot;id&quot;:req.body.id});
      const path = `cards.0.${parent.panel}`;
  
      try{
          await Card.findOneAndDelete({&quot;id&quot;: req.body.id});
          res.status(204).json({
              status: &quot;Success&quot;
          })
      } catch(err){
          res.status(300).json({
              status: &quot;Error&quot;,
              message: err.message
          })
      }
  }
  </code></pre>
  <p>Please note, <code>req.body.id</code> is the correct value, and <code>parent.panel</code> is valid.</p>
  <p>However, I test this once - POST request comes back with code <code>204</code>. Ok, great. However, my error alert is shown.</p>
  <p>So, I reload - maybe it's been deleted, as it should have been.</p>
  <p>Nope, still there. Let's try deleting the card again.</p>
  <p>Ah - an error this time:</p>
  <pre><code>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'panel' of null
      at exports.updateCardPlace
  </code></pre>
  <p>I have absolutely no idea what this is about - could anyone help me?</p>
  <p><strong>Card Schema</strong>:</p>
  <pre class="lang-js prettyprint-override"><code>const cardSchema = new mongoose.Schema({
      summary: String,
      description: String,
      tags: Array,
      urgency: String,
      id: String,
      panel: String
  });
  </code></pre>
  <p><strong>Project Schema</strong>:</p>
  <pre class="lang-js prettyprint-override"><code>const projectSchema = new mongoose.Schema({
      name: {
          type: String,
          required: [true, 'Project name is required']
      },
      type: String,
      photo: String,
      members: [String],
      code: String,
      cards: [{
          &quot;pending&quot;: { 
              type: mongoose.Schema.Types.Object,
              ref: 'Card'
          },
          &quot;inprogress&quot;: { 
              type: mongoose.Schema.Types.Object,
              ref: 'Card'
          },
          &quot;issues&quot;: { 
              type: mongoose.Schema.Types.Object,
              ref: 'Card'
          },
          &quot;completed&quot;: { 
              type: mongoose.Schema.Types.Object,
              ref: 'Card'
          }
      }],
      tags: Object
  });
  </code></pre>