title: Cannot set property 'imgId' of undefined
body: <p>I'm new to node.js and trying to save photos of users into mongodb.</p>
  
  <p>Here is the router:</p>
  
  <pre><code>  router.post('/addphoto',  checkAuth, upload.single('image'), (req, res)=&gt; {
  
     let filename = Math.floor(Math.random() * 100000)
     console.log('imgId is', filename  );
  
        const photoFields = {};
        photoFields.photos = []
      if(req.file) photoFields.image = req.file.path ;
      photoFields.user = req.user.id; 
  
      Photo.findOne({ _id: req.user.id }).then(photo =&gt; {
            if(photo) {
          //Update
  
          console.log('photo found! \n\n');
          Photo.findOneAndUpdate(
            {user: req.user.id},
            {$set: photoFields},
            {new: true}
            ).then(photo =&gt; res.json(photo));
        } else {
          console.log('No photo found! \n\n');
          //Create
          photoFields.photos[0].imgId =  filename  ;
          photoFields.photos[0].isProfileImg =  true  ;
          photoFields.photos[0].visible = 'all';
  
  
          //Save
          new Photo(photoFields).save().then(photo=&gt; {
              res.json(photo);
          }).catch(err =&gt; {
                console.log('could not save photo \n\n');
                console.log(err);
          }
  
        );
    }
      }) 
    });
  </code></pre>
  
  <p>Each user can upload several photos so here is the photo schema in which photos is an array of objects:</p>
  
  <pre><code>  const PhotoSchema = new Schema({
  
      user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
      }, 
  
    photos: [
      {
        imgId: {
          type: Number,
        },
        isProfileImg: {
          type: Boolean,
          default: true,
        },
        visible: {
          type: String,
        }
      }
    ]
  
  });
  </code></pre>
  
  <p>As you can see I create imgId using random function, and I can see that it is created:</p>
  
  <pre><code>imgId is 65609
  </code></pre>
  
  <p>However, I still get this error in the console:</p>
  
  <pre><code>(node:5295) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'imgId' of undefined
  </code></pre>
  
  <p>And no image is saved. </p>
  
  <p>I'm using multer to extract file from request post and I can set in the console that file is being received. </p>
  
  <p>I appreciate your hints to fix this error which bugs me for hours. </p>