title: Node.js mongodb and promises
body: <p>I asked my question and I got reffered to another answer but I can not manage it ;( Could someone please help me`?</p>
  
  <p>My orginal question:<a href="https://stackoverflow.com/questions/46917016/how-to-access-data-from-function-node-js">How to access data from function (node.js)</a></p>
  
  <p>I tried to do what was suggested. It works untill there is a collection in the mongodb. What would happen if there is no collection?
  Im getting an error</p>
  
  <pre><code>(node:18) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'userName' of undefined
  </code></pre>
  
  <p>Is there any nice and simple way to ensure that my function will work even there is no collection?</p>
  
  <p>My indeks.js</p>
  
  <pre><code>var userID = this.event.session.user.userId;
  console.log(userID);
  var self = this;
  DbConn.dbFind("myUsers", userID).then(function(item) {
  
      self.emit(':ask',
          SpeechOutputUtils.pickRandom(self.t('WELCOME_OK', item.userName))
      );
  
      }, function(err) {
      self.emit(':ask',
          SpeechOutputUtils.pickRandom(self.t('WELCOME'))
      );
  });
  </code></pre>
  
  <p>my db.utilis</p>
  
  <p>module.exports = {</p>
  
  <pre><code>    dbFind: function(collectionName, userID) {
              return MongoClient.connect(url).then(function(db) {
                var collection = db.collection(collectionName);
  
                return collection.findOne({alexaUserID:userID});
              }).then(function(item) {
                    return item;    
              });
            }
          };
  </code></pre>