SQLDBA SQLDBA - 6 months ago 28
JSON Question

Aurelia - If params in Promise.all

I am trying to work out a search page where a number of search types can be filled in. Then a WebApi call is made if the search type is filled in and passed as a parameter to activate().

It seems inefficient to call the api if I know that the param is null/undefined, but I am not sure how to do something like this:

if (params.myparam === undefined)
{ don't include it in the promise }
else
{ include in the promise }


Below is the code I have so far....

It all works properly as is but I end up passing "undefined" as the search parameter. That doesn't work for me because there is actually a piece of art named "The Die Maker from the series Eight Sheets from an Undefined Novel" which in this case should not show up. And "undefined" needs to be a valid search term.

activate(params) {

if(params.exhibitionID != null)
{
return this.apiData.getByExhibitionId(params.exhibitionID).then(exhibitions => { this.exhibitions = exhibitions.objects; if(exhibitions.objects.length > 0){ this.showExhibitions = "display"; this.exhibitionsCount = exhibitions.objects.length; this.exhibitionTitle = exhibitions.exhibitionTitle; } });
}
else
{
return Promise.all([
this.apiData.getBySearchDated(params.searchDated).then(dateds => { this.dateds = dateds; if(dateds.length > 0){ this.showDateds = "display"; this.datedsCount = dateds.length; } }),
this.apiData.getBySearchCreditline(params.searchCreditline).then(creditlines => { this.creditlines = creditlines; if(creditlines.length > 0){ this.showCreditlines = "display"; this.creditlinesCount = creditlines.length; } }),
this.apiData.getBySearchNumber(params.searchNumber).then(numbers => { this.numbers = numbers; if(numbers.length > 0){ this.showNumbers = "display"; this.numbersCount = numbers.length; } }),
this.apiData.getBySearchTitle(params.searchTitle).then(titles => { this.titles = titles; if(titles.length > 0){ this.showTitles = "display"; this.titlesCount = titles.length; } }),
this.apiData.getBySearchCreator(params.searchCreator).then(creators => { this.creators = creators; if(creators.length > 0){ this.showCreators = "display"; this.creatorsCount = creators.length; } })
]);
}
}


Here is what one of the api calls looks like. I put in a hack that returns empty json, but it just seems right not to make the call in the first place if I know there are no results. Any suggestions? Thanks!

API call:

getBySearchTitle(searchTitle) {

// the hack
// if(searchTitle == undefined){ searchTitle = token;}

return this.http.fetch(baseUrl + "/objects/" + token + "&search=" + searchTitle + "&searchtype=title")
.then(response => response.json())
.then(response => {

if(response.length > 1000)
{
alert("Your search returns too many results (maximum 1000). Please refine your search and try again.");
this.router.navigateBack();
}
else{
return response.objects;
}

});
}

Answer

You could check if the parameter exists before constructing the Promise. Your activate method would look something like this:

activate(params) {

    if(params.exhibitionID != null)
    {
        return this.apiData.getByExhibitionId(params.exhibitionID).then(exhibitions => { this.exhibitions = exhibitions.objects; if(exhibitions.objects.length > 0){ this.showExhibitions = "display"; this.exhibitionsCount = exhibitions.objects.length; this.exhibitionTitle = exhibitions.exhibitionTitle; } });
    }
    else
    {
        return Promise.all([
            !params.searchDated || this.apiData.getBySearchDated(params.searchDated).then(dateds => { this.dateds = dateds; if(dateds.length > 0){ this.showDateds = "display"; this.datedsCount = dateds.length; } }),
            !params.searchCreditline || this.apiData.getBySearchCreditline(params.searchCreditline).then(creditlines => { this.creditlines = creditlines; if(creditlines.length > 0){ this.showCreditlines = "display"; this.creditlinesCount = creditlines.length; } }),
            !params.searchNumber || this.apiData.getBySearchNumber(params.searchNumber).then(numbers => { this.numbers = numbers; if(numbers.length > 0){ this.showNumbers = "display"; this.numbersCount = numbers.length; } }),
            !params.searchTitle || this.apiData.getBySearchTitle(params.searchTitle).then(titles => { this.titles = titles; if(titles.length > 0){ this.showTitles = "display"; this.titlesCount = titles.length; } }),
            !params.searchCreator || this.apiData.getBySearchCreator(params.searchCreator).then(creators => { this.creators = creators; if(creators.length > 0){ this.showCreators = "display"; this.creatorsCount = creators.length; } })
        ]);
    }
}