【问题标题】:TypeScript compile error: Array.map(callback) expression is not callableTypeScript 编译错误:Array.map(callback) 表达式不可调用
【发布时间】:2020-06-08 16:10:15
【问题描述】:

以下是一个用 Typescript 编写的 Node.js API,

app.post('/photos/upload', upload.array('photos', 12), async (req, res) => {
  var response = { }
  var list = []
  try {
    const col = await loadCollection(COLLECTION_NAME, db)

    var myfiles = req.files
    console.log("myfiles", myfiles);
    myfiles.map( function(item, index) {

                //return checkExisting(col, item)
    })
}}

并在myfiles.map(function(item, index) { 行收到以下打字稿错误

index.ts:77:13 - error TS2349: This expression is not callable.
  Not all constituents of type 'File[] | (<U>(callbackfn: (value: File, index: number, array: File[]) => U, thisArg?: any) => U[])' are callable.
    Type 'File[]' has no call signatures.

77     myfiles.map( function(item, index) {
               ~~~

由于我对 Typescript 完全陌生,不确定是什么问题,myfiles.map(callback) 是数组上的有效可调用方法,所以为什么 typescript 报告不调用。

【问题讨论】:

  • 在错误行之前输入:console.log (typeof myfiles) 并在此处输入结果
  • 如果添加async (req: {files: any[]}, res)会怎样
  • 关键是看看checkExisting 做了什么。看看这个。 stackoverflow.com/a/51573837/17447
  • @naveen 已经禁用 checkExisting 并尝试但同样的错误。 checkExisting一个函数
  • @AksJacoves 这是一个打字稿编译时错误,所以console.log 没有被执行。我尝试过这个。此外,我在终端看到 npm ERR! node-tutorial@1.0.0 prestart: tsc`` 和 Failed at the node-tutorial@1.0.0 prestart script.

标签: javascript node.js typescript


【解决方案1】:

我不知道为什么会这样,但你可以试试这个:

var myfiles = JSON.parse(JSON.stringify(req.files))

看起来像这样:

app.post('/photos/upload', upload.array('photos', 12), async (req, res) => {
    var response = { }
    var list = []
    try {
        const col = await loadCollection(COLLECTION_NAME, db)

        var myfiles = JSON.parse(JSON.stringify(req.files))
        console.log("myfiles", myfiles);
        myfiles.map( function(item, index) {

            //return checkExisting(col, item)
        })
    }}

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2020-08-16
    • 1970-01-01
    • 2021-04-27
    • 2021-02-12
    • 2015-08-26
    • 2021-06-12
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多