通过 Express 向 Google Places API 发出远程请求每次都会获取重复的结果
Making remote request to Google Places API via Express fetches duplicate results everytime
我一直在尝试使用 Google 个位置 API 通过文本查询获取搜索结果。
我的 URL 字符串是
https://maps.googleapis.com/maps/api/place/textsearch/json?query=${textQuery}&&location=${lat},${lng}&radius=10000&key=${key}
来自浏览器的 GET 请求完美运行。
https://maps.googleapis.com/maps/api/place/textsearch/json?query=saravana stores&&location=13.038063,80.159607&radius=10000&key=${key}
以上搜索获取与查询相关的结果。
https://maps.googleapis.com/maps/api/place/textsearch/json?query=dlf&&location=13.038063,80.159607&radius=10000&key=${key}
此搜索还会获取与 dlf 相关的结果。
但是,当我尝试通过快速服务器执行相同操作时,它会为我提供不同查询的相同搜索结果。
app.get('/findPlaces', (req, res) => {
SEARCH_PLACES = SEARCH_PLACES.replace("lat", req.query.lat);
SEARCH_PLACES = SEARCH_PLACES.replace("lng", req.query.lng);
SEARCH_PLACES = SEARCH_PLACES.replace("searchQuery", req.query.search);
https.get(SEARCH_PLACES, (response) => {
let body = '';
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
let places = JSON.parse(body);
const locations = places.results;
console.log(locations);
res.json(locations);
});
}).on('error', () => {
console.log('error occured');
})
});
从客户端,如果我向 /findPlaces?lat=13.038063&lng=80.159607&search=saravana stores
发出第一个请求,我会得到正确的结果。当我尝试像 [search=dlf]
这样的不同搜索时,它给我的结果与我从 [search=saravana stores]
得到的结果相同。我什至尝试使用不同的查询搜索来搜索不同的纬度、经度。
但是,如果我重新启动我的节点服务器,则会获取正确的结果。实际上,我无法为每个新请求重新启动服务器。
我错过了什么吗?请帮忙。
谢谢。
问题是您正在用第一个查询替换全局变量 SEARCH_PLACES
。之后,您不能再次替换占位符,因为它们已经在该字符串中被替换了。
例如,当应用程序启动时 SEARCH_PLACES
具有此值:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=searchQuery&location=lat,lng&radius=10000
第一次请求后,全局变量将变为:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=foo&location=13,37&radius=10000
当第二个请求进来时,字符串中不再有任何占位符可以替换,因此最后一个请求再次返回。
您想要构造 URL 而不为每个请求修改全局请求:
const SEARCH_PLACES = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
app.get('/findPlaces', (req, res) => {
const { lat, lng, search } = req.query
let url = `${SEARCH_PLACES}?query=${search}&location=${lat},${lng}`
https.get(url, (res) => {
// ...
})
})
我一直在尝试使用 Google 个位置 API 通过文本查询获取搜索结果。
我的 URL 字符串是
https://maps.googleapis.com/maps/api/place/textsearch/json?query=${textQuery}&&location=${lat},${lng}&radius=10000&key=${key}
来自浏览器的 GET 请求完美运行。
https://maps.googleapis.com/maps/api/place/textsearch/json?query=saravana stores&&location=13.038063,80.159607&radius=10000&key=${key}
以上搜索获取与查询相关的结果。
https://maps.googleapis.com/maps/api/place/textsearch/json?query=dlf&&location=13.038063,80.159607&radius=10000&key=${key}
此搜索还会获取与 dlf 相关的结果。
但是,当我尝试通过快速服务器执行相同操作时,它会为我提供不同查询的相同搜索结果。
app.get('/findPlaces', (req, res) => {
SEARCH_PLACES = SEARCH_PLACES.replace("lat", req.query.lat);
SEARCH_PLACES = SEARCH_PLACES.replace("lng", req.query.lng);
SEARCH_PLACES = SEARCH_PLACES.replace("searchQuery", req.query.search);
https.get(SEARCH_PLACES, (response) => {
let body = '';
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
let places = JSON.parse(body);
const locations = places.results;
console.log(locations);
res.json(locations);
});
}).on('error', () => {
console.log('error occured');
})
});
从客户端,如果我向 /findPlaces?lat=13.038063&lng=80.159607&search=saravana stores
发出第一个请求,我会得到正确的结果。当我尝试像 [search=dlf]
这样的不同搜索时,它给我的结果与我从 [search=saravana stores]
得到的结果相同。我什至尝试使用不同的查询搜索来搜索不同的纬度、经度。
但是,如果我重新启动我的节点服务器,则会获取正确的结果。实际上,我无法为每个新请求重新启动服务器。
我错过了什么吗?请帮忙。
谢谢。
问题是您正在用第一个查询替换全局变量 SEARCH_PLACES
。之后,您不能再次替换占位符,因为它们已经在该字符串中被替换了。
例如,当应用程序启动时 SEARCH_PLACES
具有此值:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=searchQuery&location=lat,lng&radius=10000
第一次请求后,全局变量将变为:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=foo&location=13,37&radius=10000
当第二个请求进来时,字符串中不再有任何占位符可以替换,因此最后一个请求再次返回。
您想要构造 URL 而不为每个请求修改全局请求:
const SEARCH_PLACES = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
app.get('/findPlaces', (req, res) => {
const { lat, lng, search } = req.query
let url = `${SEARCH_PLACES}?query=${search}&location=${lat},${lng}`
https.get(url, (res) => {
// ...
})
})