导入同一文件封装的相似函数后,一个成功应用,一个报错?
发布于 6 年前 作者 xiuyinglong 15245 次浏览 来自 问答

//模块文件 home_api.js

function getWeatherDataNow(latitude,longitude){

let type = "now";

return getWeatherData(latitude, longitude, type);

}

function getWeatherDataForcast(latitude, longitude){

let type = "forecast";

return getWeatherData(latitude, longitude, type);

}

module.exports = {

getWeatherDataNow : getWeatherDataNow,

getWeatherDataForcast : getWeatherDataForcast

}

//在 pages/home/home.js 中使用

导入方式:

var weatherReq = require("./../../service/home_api.js");

........

//应用位置

onLoad: function(options) {

//1.读取位置信息

let that = this;

console.log(that);

wx.getLocation({

type: 'wgs84',

success: function(res) {

//根据位置信息获取天气信息与城市信息

weatherReq.getWeatherDataNow(res.latitude, res.longitude).then(

(weather_now) => {

console.log(weather_now);

});

weatherReq.getWeatherDataForecast(res.latitude, res.longitude).then(

(weather_forecast) => {

console.log(weather_forecast);

});

}

});

},



报错内容:

VM7979:1 thirdScriptError

weatherReq.getWeatherDataForecast is not a function;at pages/home/home onLoad function;at api getLocation success callback function

TypeError: weatherReq.getWeatherDataForecast is not a function

    at success (http://127.0.0.1:63406/appservice/pages/home/home.js:51:20)


请问这是什么原因?


1 回复

虽然不知道为什么,但是把

module.exports = {

getWeatherDataNow : getWeatherDataNow,

getWeatherDataForcast : getWeatherDataForcast

}

改为 :

module.exports.WeatherDataNow = getWeatherDataNow;

module.exports.WeatherDataForcast = getWeatherDataForcast;

就可以了

回到顶部