From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Handle geocode error

Handle geocode error

All right, guys, so we have the maps displaying. However, the map is going to display if the address is correct, and it's there. So let's handle the case where it can't translate an address to latitude and longitude through geocoding. So if we go to one of our listings and just temporarily just put in some gibberish for the location and then update that, now you'll see it's just going to give us this constant loading spinner. So we want to handle that case. So in our property map, let's add a new piece of state. So we'll say const. And let's call this, we'll just call it geocode error. And then set geocode error. And set that to use state. And that's going to be false by default. And then down here in our use effect where we make our request in this fetch chords, Let's wrap this stuff in a try-catch. So I'm going to grab that, cut it, and then open up a try-catch block, and then put that in the try. Now, we want to check the response here. And if the results array has nothing in it…

Contents