简体   繁体   English

类型错误:Webpack 导入的模块不是 function

[英]TypeError: Webpack imported module is not a function

I have a backend that calculates work shifts.我有一个计算工作班次的后端。 I am trying to post some required user input with a module in services/shifts.我正在尝试使用服务/班次中的模块发布一些必需的用户输入。 The getAll method works fine, but posting throws an error getAll 方法工作正常,但发布会引发错误

TypeError: _services_shifts__WEBPACK_IMPORTED_MODULE_2__.default.postData is not a function类型错误:_services_shifts__WEBPACK_IMPORTED_MODULE_2__.default.postData 不是 function

Shiftservice module:换档服务模块:

import axios from 'axios'
const baseUrl = '...'

const getAll = () => {
    const request = axios.get(baseUrl)
    return request.then(response => response.data)
}
const postData = newObject => {
    const request = axios.post(baseUrl, newObject)
    return request.then(response => response.data)
}

export default {getAll, postData}

I have a button that triggers the following calling code on click:我有一个按钮,可以在单击时触发以下调用代码:

import shiftService from './services/shifts'

  const postData = (event) => {
    event.preventDefault()
    const sampleObject = {
      sampleField: sample
    }
    shiftService
      .postData(sampleObject)
      .then(returnedData => {
        console.log(returnedData)
      })
}

When execution reaches shiftService.postData, the error is thrown.当执行到 shiftService.postData 时,抛出错误。

I am really confused since I am basically copying some older project of mine which works, but here I just don't find the problem.我真的很困惑,因为我基本上是在复制我的一些有效的旧项目,但在这里我只是找不到问题。 Thank you in advance for helping a newcomer!预先感谢您帮助新人!

Modules provide special export default (“the default export”) syntax to make the “one thing per module” way look better.模块提供特殊的默认导出(“默认导出”)语法,使“每个模块一个东西”的方式看起来更好。 There may be only one export default per file .And we may neglect the name of the class in the following example.每个文件可能只有一个导出默认值。在下面的示例中我们可能会忽略 class 的名称。

 //Module1
export default class{
}

And then import it without curly braces with any name:然后导入它而不用任何名称的大括号:

//Module2
import anyname from './Module1' 

Your scenario is different having two functions.You can either export default one function您的情况不同,有两个功能。您可以导出默认一个 function

export default getAll

and normal export the other function.并正常导出另一个function。

export postData

and when importing并且在导入时

import{ default as getAll,postData} from './yourModule'

OR要么

Remove default here in Shiftservice module and export normally:在 Shiftservice 模块中去掉这里的default并正常导出:

import axios from 'axios'
const baseUrl = '...'

const getAll = () => {
    const request = axios.get(baseUrl)
    return request.then(response => response.data)
}
const postData = newObject => {
    const request = axios.post(baseUrl, newObject)
    return request.then(response => response.data)
}

export {getAll, postData}

Importing in your module在你的模块中导入

import {getAll,PostData} from './Module1'

or要么

import * as shiftService from './Module1'

and then use shiftServer.postData() .....然后使用shiftServer.postData() .....

Okay, I am embarrassed of the solution.好的,我对解决方案感到尴尬。 I was just editing an earlier version of shiftService from a wrong folder, and the imported service only had the get method in it...我只是从错误的文件夹中编辑早期版本的 shiftService,而导入的服务中只有 get 方法...

So my code actually works if placed correctly.所以如果放置正确,我的代码实际上可以工作。 Thank you for your time, and thanks for sharing alternative ways that must work aswell.感谢您的宝贵时间,也感谢您分享同样有效的替代方法。

I think its becuse your declaring the function as arrow functions and export it this way:我认为这是因为您将 function 声明为箭头函数并以这种方式导出:

export default {getAll, postData}

you need to declare them as a normal functions您需要将它们声明为普通函数

function postData(){
}

that should work那应该工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack 编译错误:TypeError: __WEBPACK_IMPORTED_MODULE_1__ ... is not a function - Webpack compile error: TypeError: __WEBPACK_IMPORTED_MODULE_1__ … is not a function TypeError:react__WEBPACK_IMPORTED_MODULE_6 ___ default.a.useState不是一个函数 - TypeError: react__WEBPACK_IMPORTED_MODULE_6___default.a.useState is not a function 类型错误:moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort 不是函数 - TypeError: moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort is not a function 是的验证库类型错误:是的__WEBPACK_IMPORTED_MODULE_0__ 不是函数 - yup validation library TypeError: yup__WEBPACK_IMPORTED_MODULE_0__ is not a function 类型错误:标记为__WEBPACK_IMPORTED_MODULE_3___default(...) 不是函数 - TypeError: marked__WEBPACK_IMPORTED_MODULE_3___default(...) is not a function "类型错误:_fire__WEBPACK_IMPORTED_MODULE_1__.default.auth 不是函数" - TypeError: _fire__WEBPACK_IMPORTED_MODULE_1__.default.auth is not a function 类型错误:_models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find 不是函数 - TypeError: _models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find is not a function 类型错误:_fire__WEBPACK_IMPORTED_MODULE_11__.default.collection 不是 function - TypeError: _fire__WEBPACK_IMPORTED_MODULE_11__.default.collection is not a function 类型错误:_firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection 不是 function - TypeError: _firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection is not a function 未捕获的类型错误:_firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth 不是 function - Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM