我想使用谷歌地圖的地方API來構建一個簡單的Web應用程序。現在我只是將從api獲得的數據插入到我的mongodb數據庫中。谷歌地圖API和表達錯誤發送後無法設置標題
谷歌地圖的地方npm模塊@google/maps
有一個基於經度和緯度的返回數據的地方法。我試圖實現的是這個
- 當對api路由發出請求時,它應該檢查數據庫並返回一些數據。
- 如果數據庫爲空應該查詢谷歌地圖放置數據的API,將其插入數據庫,然後從數據庫執行查找請求。這裏是我的[代碼] [1]中,錯誤的是:
Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11) at ServerResponse.header (C:\Express\pizza\node_modules\express\lib\response.js:730:10) at ServerResponse.send (C:\Express\pizza\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Express\pizza\node_modules\express\lib\response.js:256:15) at sendJsonResponse (C:\Express\pizza\app_api\controllers\locations.js:9:9) at C:\Express\pizza\app_api\controllers\locations.js:73:39 at Query.<anonymous> (C:\Express\pizza\node_modules\mongoose\lib\model.js:3800:16) at C:\Express\pizza\node_modules\kareem\index.js:277:21 at C:\Express\pizza\node_modules\kareem\index.js:131:16 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9)
是否有更好的方法來解決我的問題,我願意接受建議
var mongoose = require('mongoose');
var Location = require('../models/locations');
var googleMapsClient = require('@google/maps').createClient({
key: process.env.MAP_API_KEY
});
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
//function to filter data returned from api
var buildLocations = function(results) {
results === Array;
var newList = [];
var j = 3;
for (var i = 0; i < j; i++) {
if (results[i].photos) {
newList.push({
place_id: results[i].place_id,
name: results[i].name,
rating: results[i].rating,
address: results[i].formatted_address,
photos: results[i].photos,
location: results[i].geometry.location
})
}
}
return newList
}
//function to save data to database from api
var saveToDbFromApi = function(locations,req,res,callback){
for(var i=0; i<locations.length; i++){
Location.create({
name: locations[i].name
},function(err,locations){
if(err){
console.log(err);
}
else{
callback(req,res);
}
})
}
}
module.exports.locationList = function(req, res) {
Location.find({}, function(err, locations) {
if (err) {
console.log(err);
sendJsonResponse(res, 400, err);
}
if (!locations.length) {
googleMapsClient.places({
location: [5.526679, -0.48085],
radius: 50000,
type: 'restaurant'
}, function(err, response){
if(err){
sendJsonResponse(res,400,err);
}
else{
var locations = buildLocations(response.json.results);
saveToDbFromApi(locations,req,res,function(req,res){
Location.find({},function(err,locations){
if(err){
console.log(err)
}
else{
\t \t \t \t \t \t \t //this line brings up the 'cant set headers after they are sent error'
return sendJsonResponse(res,200,locations);
}
})
});
}
})
} else {
sendJsonResponse(res, 200, locations);
}
})
}
您正在嘗試將數據發佈到已關閉的響應。我們可以看到你的代碼嗎?單獨的堆棧跟蹤將無濟於事。我們需要查看進行調用的服務器端代碼。 – magreenberg
我發佈了一個代碼鏈接,你可以在這裏查看[鏈接] https://pastebin.com/bCEepQ1Z –