【问题标题】:Uncaught TypeError: x is not a function when Importing Factory FunctionUncaught TypeError: x is not a function when Importing Factory Function
【发布时间】:2022-01-09 04:23:51
【问题描述】:

我有一个名为 updateUI 的工厂函数,我将其导入 index.js。导入时会识别 updateUI 的实例,但是在尝试调用 updateUI 的方法时出现错误。

将所有代码放入 index.js 可以消除错误,但仍然让我摸不着头脑。似乎只有在导入时才会出现问题。为什么方法不被识别?

index.js

import '../../dist/output.css';
import { updateUI } from './display.js';
    
    
const test = updateUI('Daly City');
test.setBackground();

display.js

import { getWeather } from './apiCall';

const updateUI = async(location) => {
    const res = await getWeather(location, process.env.apiKEY);

    function updateCity() {
        const city = document.querySelector('#location');
        city.innerHTML = res.name;
    };

    function updateCurrentTemp() {
        const currentTemp = document.querySelector('#current-temp');
        const farenheit = Math.round((9/5)*(res.main.temp - 273) + 32);
        currentTemp.innerHTML = farenheit;
    };

    function updateConditions() {
        const condition = document.querySelector('#weather');
        condition.innerHTML = res.weather[0].description;
    };

    function setBackground() {
        const date = new Date();
        let time = date.getTime();
        console.log(time);
    };

    return { updateCity, updateCurrentTemp, updateConditions, setBackground }
};

export { updateUI };

【问题讨论】:

  • 请在此处复制您的代码,而不是发布它的图像。这将使我们更容易提供帮助。
  • @GenericUser 已更新
  • Uncaught TypeError:... 你错过了最重要的部分,错误本身!请包括它。
  • updateUI 是异步的并返回一个承诺。您试图在返回值 setBackground() 之前获取它。在async 函数或.then() 中使用await
  • @code 啊,我明白了,我认为 updateUI 中的原始 async..await 会“继承”到 index.js。非常感谢你。仍然试图围绕承诺缠绕我的头。

标签: javascript webpack ecmascript-6 import


【解决方案1】:

@code 在 cmets 中回答的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多