我一直在嘗試創(chuàng)建一個function returnCoords(par1)用戶可以在其上輸入文本輸入,并使用node-geocodernpm 將其轉(zhuǎn)換為坐標(biāo)的方法。盡管該函數(shù)看起來工作正常,但在返回該函數(shù)時它會給出undefined結(jié)果。我嘗試用它來解決這個問題,async但await無論有沒有這些,似乎都沒有任何效果。我也嘗試過傳遞一個對象/數(shù)組(兩個文本), geocoder.geocode(array/object, async function(err, data){...})但似乎也不起作用,因為它只接受String值。var express = require('express');var router = express.Router();var nodeGeocoder = require('node-geocoder');var geocoderOptions = { provider: "google", httpAdapter: "https", apiKey: process.env.GEOCODER_API_KEY, //collect key from hidden environment variable formatter: null}var geocoder = nodeGeocoder(geocoderOptions);router.post('/updateMap', async (req, res) => { //GET USER TEXT (MAP) INPUT & convert to coordinates //req.body.mapFrom: "LA, USA" var from_coords = await returnCoords(req.body.mapFrom); console.log("TEST 1:" + typeof(from_coords)); console.log("TEST 2:" + from_coords); //console.log("TEST 3:" + from_coords[0]); //console.log("TEST 4:" + from_coords[0].latitude); //console.log("TEST 5:" + from_coords[0].longitude); });async function returnCoords(userInput){ geocoder.geocode(userInput, async function(err, data){ if(err || !data.length) { req.flash('error', 'Invalid address'); return res.redirect('back'); } console.log("LAT:" + data[0].latitude); console.log("LNG:" + data[0].longitude); return data; });}CONSOLE LOG 按以下順序打?。篢EST 1 : undefinedTEST 2 : undefinedLAT : 34.0522342LNG : -118.2436849
如何在同一個發(fā)布請求中多次使用地理編碼功能(npm:node-geocoder)?
慕哥9229398
2022-10-27 15:43:09