I asked my question and I got reffered to another answer but I can not manage it ;( Could someone please help me`?
My orginal question:How to access data from function (node.js)
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
(node:18) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'userName' of undefined
Is there any nice and simple way to ensure that my function will work even there is no collection?
My indeks.js
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'))
);
});
my db.utilis
module.exports = {
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;
});
}
};