google-maps-services-js icon indicating copy to clipboard operation
google-maps-services-js copied to clipboard

Error 400 when trying to load next page

Open mbpictures opened this issue 4 years ago • 3 comments

Getting an error 400 when specifying the "pagetoken" in PlacesNearbyRequest.

Environment details

  1. Specify the API at the beginning of the title: PlacesNearby
  2. OS type and version: Windows 10
  3. Library version and other environment information: 3.1.13

Steps to reproduce

  1. Create a nearby search request
  2. Create a second search request using the nextpagetoken from the first request
  3. (Optionally: wait one second using the setTimeout function)

Code example

async loopPages(currentResponse: ParsedPlaceResponse, location: Location): Promise<PlaceResult> {
        return new Promise<PlaceResult>(async (resolve, reject) => {
            let places = [];
            if (currentResponse.nextPage !== undefined) {
                console.log("NEXT PAGE. Token: " + currentResponse.nextPage);
                try {
                    let placeRequest = await this.loadPlaces({
                        params: {
                            key: GooglePlaceProvider.GOOGLE_API_KEY,
                            pagetoken: currentResponse.nextPage,
                            location: location
                        }
                    }, location);
                    currentResponse.nextPage = placeRequest.nextPage;
                    places.push(...(await this.loopPages(currentResponse, location)));
                } catch(e) {
                    console.log(e);
                    await setTimeout(async () => places.push(...(await this.loopPages(currentResponse, location))), 1000);
                }
            }
            resolve(places);
        });
    }

Stack trace

Request failed with status code 400
- node_modules\@googlemaps\google-maps-services-js\node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\@googlemaps\google-maps-services-js\node_modules\axios\lib\core\settle.js:16:9 in settle
* http://192.168.178.100:19001/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:172585:14 in <unknown>
- node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

Following these steps will guarantee the quickest resolution possible.

Thanks!

mbpictures avatar Nov 03 '20 15:11 mbpictures

Same problem, when entering failing GET request directly in the browser it works.

Adding a setTimeout() fixes the problem.

It would be interesting to have a better error message.

julesbou avatar Nov 11 '20 16:11 julesbou

@julesbou I already tried it with a timeout, but I'm receiving the same error.

mbpictures avatar Nov 11 '20 16:11 mbpictures

Any further information on this?

mbpictures avatar Dec 04 '20 19:12 mbpictures

Based on this: https://issuetracker.google.com/issues/35826470?pli=1

This is because of design and it's expected. In my case, the wait time was 2 seconds.

joseglego avatar Feb 06 '23 10:02 joseglego

As mentioned by @joseglego, this is working as intended.

wangela avatar Jun 21 '23 07:06 wangela