Javascript 尝试向外部API发出请求时获取axios错误
这就是错误:似乎错误在axios请求中,但我不确定。。 奇怪的是,当我在《邮递员》中测试这个请求时,我得到了干净漂亮的回应Javascript 尝试向外部API发出请求时获取axios错误,javascript,node.js,axios,Javascript,Node.js,Axios,这就是错误:似乎错误在axios请求中,但我不确定。。 奇怪的是,当我在《邮递员》中测试这个请求时,我得到了干净漂亮的回应 UnhandledPromiseRejectionWarning: Error: Request failed with status code 404 at createError (C:\Users\lusyv\OneDrive\Documents\Elevation\projects\weatherApp\node_modules\axios\lib\core
UnhandledPromiseRejectionWarning: Error: Request failed with status code 404
at createError (C:\Users\lusyv\OneDrive\Documents\Elevation\projects\weatherApp\node_modules\axios\lib\core\createError.js:16:15)
at settle (C:\Users\lusyv\OneDrive\Documents\Elevation\projects\weatherApp\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (C:\Users\lusyv\OneDrive\Documents\Elevation\projects\weatherApp\node_modules\axios\lib\adapters\http.js:244:11)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:13644) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not
handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:13644) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
我试图通过localhost服务器从外部API渲染天气数据,在单击“搜索按钮”时出现此错误
这是简单的MVC架构,下面是我的代码:
server.js
router.get(`/city/:cityName`, async (req, res) => {
let cityName = req.params.cityName
console.log("LU")
const response = await axios.get(`http://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=metric&appid=ec035401d1d40d6f5bb07d9588812996`)
let city = {
name: response.data.name,
temprature: response.data.main.temp,
condition: response.data.weather[0].description,
conditionPic: response.data.weather[0].icon, //http://openweathermap.org/img/wn/${icon}@2x.png
wind: response.data.wind.speed
}
res.send(city)
})
model.js
class TempManager {
constructor(){
this.cityData = []
}
async getDataFromDB(){
let citiesData = await $.get(`/cities`)
if(citiesData){
this.cityData = citiesData
}
return(this.cityData)
}
async getCityDataFromExtAPI(cityName){
let city = await $.get(`/city/:${cityName}`)
console.log(city)
this.cityData.push(city)
return(this.cityData)
}
}
controller.js
let manager = new TempManager()
let renderer = new Renderer()
const loadPage = async () => {
let citiesDataFromDB = await manager.getDataFromDB()
renderer.renderData(citiesDataFromDB)
}
const handleSearch = async (userInput) => {
console.log(userInput)
let city = await manager.getCityDataFromExtAPI(userInput)
console.log(city)
renderer.renderData(city)
}
$(`#search-btn`).on("click", async function () {
let userInput = $(`#city-input`).val()
await handleSearch(userInput)
})
render.js
class Renderer {
constructor(){}
renderData(allCityData){
const source = $('#first-template').html()
const template = Handlebars.compile(source)
const newHTML = template({city: allCityData})
$("#city").empty()
$('#city').append(newHTML)
}
}
index.html
</head>
<input type="text" id="city-input">
<button id="search-btn">Search</button>
<script id="first-template" type="text/x-handlebars-template">
{{#each allCityData}}
<div id="city">
<p id="name">{{this.name}}</p>
<p id="temprature">{{this.temprature}}</p>
<p id="conditions">{{this.conditions}}</p>
<img src="{{this.conditionPic}}" alt="condition.jpg" id="image">
<p id="wind">{{this.wind}}</p>
<button id="save-btn">Save</button>
<button id="remove-btn">Remove</button>
</div>
{{/each}}
</script>
<body>
搜寻
{{{#每个都是城市数据}
{{this.name}
{{this.temprature}
{{this.conditions}
{{this.wind}
拯救
去除
{{/每个}}
我做错了什么?An的意思是“找不到”。首先调试实际使用的url。对于使用
等待时需要尝试/捕获的未处理承诺拒绝。谢谢!在添加try/catck之后,承诺拒绝被拒绝,但我仍然得到错误404。我使用Postman测试get请求并得到响应:``{“name”:“London”,“temprature”:24.23,“condition”:“clear sky”,“conditionPic”:“01d”,“wind”:4.6}```我刚刚在node.js中尝试了这个请求axios.get('http://api.openweathermap.org/data/2.5/weather?q=london&units=metric&appid=ec035401d1d40d6f5bb07d9588812996“)
而且它工作得很好,所以对于失败的一个来说有些不同。$中的冒号。get(
/city/:${cityName})
看起来非常可疑,很可能出现在req.params.cityName
:)))))冒号是多余的!爱stackoverflow及其用户。