0

I'm running a function in mounted() to grab files from my Dropbox account using a promise. On success of this promise, I loop all files and run another promise function to grab additional info of each files and add it to object.

data () {
    return {
        results: [],
        dropbox: [],
    }
},
mounted() {
    dropbox.filesListFolder({path: '/wallpapers'}).then(this.successHandler)
    this.dropbox = dropbox
},
methods: {
    successHandler (response) {
        const files = response.entries;
        async function processArray(files) {
            for (const item of files) {
                item['meta'] = await this.getFileInfo(item.id);
            }
        }
        processArray(files);
        this.results = files;
    }
    getFileInfo (id) {
        this.dropbox.filesGetMetadata({
            path: id,
        })
        .then(this.successFileInfo)
    },
    successFileInfo (response) {
        return response; 
    }
}

But this return an error:

Cannot read property 'getFileInfo' of undefined

2 Answers 2

3

You have a scope-problem - wrong this:

    let vm = this;
    async function processArray(files) {
        for (const item of files) {
            item['meta'] = await vm.getFileInfo(item.id);
        }
    }

Or you can do:

processArray.bind(this)(files);

UPD (from comments):

You forgot return in getFileInfo-method

getFileInfo (id) {
    return this.dropbox.filesGetMetadata({
        path: id,
    })
    .then(this.successFileInfo)
}
Sign up to request clarification or add additional context in comments.

4 Comments

Both suggestions fixed the error. But now the successFileInfo method return undefined instead response object. I confirm that console.log(response); inside the method return the object.
getFileInfo-method returns nothing ;)
Return an object from successFileInfo(). await vm.getFileInfo(item.id) is not waiting for this function return?
Fixed! Thanks ;)
1

When you call item['meta'] = await this.getFileInfo(item.id);, this refers to the scope of the processArray function and not the vue component.

If I'm not mistaken, you should just be able to do:

async successHandler (response) {
    const files = response.entries;
    for (const item of files) {
        item['meta'] = await this.getFileInfo(item.id);
    }
    processArray(files);
    this.results = files;
}

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.