0

Following is a Node.js API written in Typescript,

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)
    })
}}

and getting the following typescript error at the line 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) {
               ~~~

As I am completely new to Typescript, not sure, what is the problem, myfiles.map(callback) is a valid callable method on an array so why typescript reports not callbale.

11
  • 3
    Before the error line put: console.log (typeof myfiles) and type the result here Commented Jun 8, 2020 at 16:14
  • 1
    What if you add async (req: {files: any[]}, res) Commented Jun 8, 2020 at 16:17
  • the key would be to see what checkExisting does. Take a look at this. stackoverflow.com/a/51573837/17447 Commented Jun 8, 2020 at 16:17
  • @naveen Already disabled checkExisting and tried but same error. checkExisting a function Commented Jun 8, 2020 at 16:18
  • @AksJacoves It's a typescripts compile time error so console.log is not executed. I tried it. Moreover I see in the terminal npm ERR! [email protected] prestart: tsc`` and Failed at the [email protected] prestart script. Commented Jun 8, 2020 at 16:22

1 Answer 1

1

I'm not sure why that happens, but you can try this:

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

It would look something like this:

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)
        })
    }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.