0

I am having issues about getting data when check date availability, the data gave back to me not correctly

Here is my code base:

searchFakeMonthlyAvailability(query: FormData): Promise<Results> {
    return new Promise<Results>((resolve, reject) => {
        setTimeout(() => {
            let srStr: string = JSON.stringify(searchMonthlyResults);
            let sr: Results = JSON.parse(srStr);
            console.log('srStr: ', srStr)
            console.log("sr: ", sr)
            sr.monthlyresults = sr.monthlyresults.filter(result => moment(result.date).toDate().getMonth() === moment(query.startDate).toDate().getMonth())
            resolve(sr);
        }, 1500);
    });
}

so when I console.log(stStr) this is what i got:

{"filters":{"roomType":[{"value":"2BR","description":"2 Bedroom","priority":800,"minimumPoints":650},{"value":"3BR","description":"3 Bedroom","priority":900,"minimumPoints":800}],"views":[{"value":"GV","description":"Garden View","minimumPoints":650},{"value":"OS","description":"Ocean Side","minimumPoints":650},{"value":"OV","description":"Ocean View","minimumPoints":650}],"activities":[],"area":[{"name":"St. Kitts and Nevis","continent":"Caribbean","states":[{"name":"","cities":["St. Kitts"]}]}]},"results":[],"monthlyresults":[{"date":"20211120","minimumPoints":925},{"date":"20211117","minimumPoints":1050},{"date":"20211129","minimumPoints":750},{"date":"20211116","minimumPoints":1050},{"date":"20211111","minimumPoints":925},{"date":"20211101","minimumPoints":650},{"date":"20211127","minimumPoints":925},{"date":"20211108","minimumPoints":1050},{"date":"20211119","minimumPoints":1100},{"date":"20211107","minimumPoints":850},{"date":"20211118","minimumPoints":1500},{"date":"20211102","minimumPoints":650},{"date":"20211128","minimumPoints":750}],"dailyresults":[],"minimumNights":0,"maxOutMonth":"20220906"}

and when I do console.log(sr): this is what I got:

dailyresults: [] filters: activities: [] area: [{…}] roomType: Array(2) 0: {value: '2BR', description: '2 Bedroom', priority: 800, minimumPoints: 650} 1: {value: '3BR', description: '3 Bedroom', priority: 900, minimumPoints: 800} length: 2 [[Prototype]]: Array(0) views: Array(3) 0: {value: 'GV', description: 'Garden View', minimumPoints: 650} 1: {value: 'OS', description: 'Ocean Side', minimumPoints: 650} 2: {value: 'OV', description: 'Ocean View', minimumPoints: 650} length: 3 [[Prototype]]: Array(0) [[Prototype]]: Object maxOutMonth: "20220906" minimumNights: 0 monthlyresults: [] monthlyunavailableresults: (3) [{…}, {…}, {…}] results: []

my data missing monthlyresults. How can I fix this ?

Updated

Nhan Nguyen
  • 355
  • 1
  • 6
  • 24
  • looks like your filter() isn't returning anything, have you checked that date creation is happening as expected? – pilchard Oct 29 '21 at 00:03
  • What's the value of `moment(query.startDate).toDate().getMonth()`? – Barmar Oct 29 '21 at 00:11
  • BTW, doesn't moment.js have its own method to get the month? Why convert it to a date first? – Barmar Oct 29 '21 at 00:12
  • @pilchard It did return value I just updated the code – Nhan Nguyen Oct 29 '21 at 00:13
  • The console has a live reference to the object. So when you replace `sr.monthlyresults` with the filtered array, that will change what you see in the console. – Barmar Oct 29 '21 at 00:14
  • @Barmar value of `moment(query.startDate).toDate().getMonth()` is 3 – Nhan Nguyen Oct 29 '21 at 00:18
  • 2
    All the dates in the JSON have month = 10. So none of the months match and the filtered array is empty. That's what you see in your console.log. – Barmar Oct 29 '21 at 00:23

0 Answers0