简体   繁体   中英

es6 exported function “not a function”

I want to create a library file where I pass in a value and it returns an object that calls a created instance of localforage.

EDITED: simplified code still doesn't work

export default function(walkId) {
    var store = localforage.createInstance({
        name: walkId
     });

     var tilesDb = {
        test: 'val'
     };

     return tilesDb;
 }

and I import it like this:

import getTilesDb from './tilesDb';

but when I call it:

let tilesDB = getTilesDb(someIdhere);

I get an error TypeError: Object(...) is not a function

What am I doing wrong? Why can't I call my function?

You cannot export an undefined variable. In the way you have written your code, getTitlesDb is not defined yet.

There are two possible solutions. The first one is to name your function on the same line as the export statement.

export default function getTilesDb(walkId) { ... }

The second one is to declare the function as a variable and then export the variable.

const getTilesDb = function(walkId) { ... }

export default getTilesDb;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM