【发布时间】:2020-06-09 15:23:53
【问题描述】:
我正在学习 node.js,下面是我的相同代码
app.js:
const forecastService = require('./forecast')
forecastService('New York', (error,data) => {
if(error){
console.log('Error in getWeatherForecast ')
}else {
console.log('data '+data)
}
})
forecast.js:
const request = require('request')
const getWeatherForecast = (city,callback) => {
console.log('printing typeof callback ..'+typeof callback)
// prints function
console.log('printing callback ..'+callback)
//prints actual function definition
const api = 'http://api.weatherstack.com/current?access_key=abcdefghijklmn'
const weather_url = api + '&request='+city
request({url:weather_url, json:true} , (error,response,callback) => {
if(error){
callback('unable to connect to weather service',undefined)
}else (response.body.error){
const errMessage = 'Error in Response :'+response.body.error.info
console.log(errMessage)
callback(errMessage,undefined) // getting error here
}
})
}
module.exports = getWeatherForecast
问题:
在 forecast.js 的 callback(errMessage,undefined) 行,我收到错误 - TypeError: callback is not a function
我还在 Forecast.js 中将回调打印为 typeof callback = function 和 callback = actul function definition
但我仍然不知道错误是什么。
有人可以帮忙吗?
我浏览过像TypeError : callback is not a function 这样的公开帖子,所有人都说回调没有作为参数正确传递,这对我来说似乎不是这样
【问题讨论】: