I'm trying to use 2 promises, the second promise depends on the first one. And a 3 promise depends on both promises. But when first promise fails I get this error in the second promise:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'name' of null
This is my code:
var Asset = require('../models/index').Asset;
var Price = require('../models/index').Price;
var currency = req.params.currency;
var values = {
where: { slugify_url: currency },
};
// get asset information
var promisse_asset = Asset.findOne(values);
// get prices information on this asset
var promisse_prices = promisse_asset.then(function(asset) {
console.log(asset); // outputs null
// some processing
if (!!asset) {
...
});
return Promise.all([promisse_asset, promisse_prices]).then(function([asset, results]) {
...
How can I fix this? How can I handle when I don't find an Asset in my database?