Search code examples
javascriptgoogle-apigoogle-drive-apigoogle-picker

Gapi.client.drive.files.update() Returning error "Parse Error"


Working with the google api, and I have to sets of code that are essentially the same, but are producing different results.

Both simply take in a fileId and two parentId's, one to remove, and one to add with the google api. It's essentially a move operation for a file in google drive. The code that works, simply expects a signature of (from, to, file) => {} and the one that doesn't is being passed the fields in the callback of a Google Picker Call. (Link to Picker API Docs)

The first one returns a success and the second get's a response from the api stating there was a parse error, with little much else as to what could be causing the parse error.

Examples:

Working Example

moveFile = (from, to, file) => {
    return gapi.client.drive.files
        .update({
            addParents: to,
            removeParents: from,
            fileId: file,
        })
        .then((res) => {
            logger.log(funcname, `Moved File: ${file} from ${from} to ${to}.`);
            return res.result;
        });
};

Non-Working Example

function folderSelectedCallback(folderData, fileData) {

    //folderData and fileData both have the same structure
    //{action: 'type of action', 
    //docs: [{Array Of Document Objects}],
    //viewToken: [Array of unused data]}

    //Docs Array Objects Structure (only used properties listed.)
    //{ id: 'string ID of the File',
    //parentId: 'string ID of the parent folder'}

    if (folderData.action === 'picked') {
        console.log('folderSelectedCallback(): called.');
        console.log('File Data: ', fileData);
        console.log('Folder Data: ', folderData);
        const files = fileData.docs;
        const folder = folderData.docs[0];

        console.log(files);
        console.log(folder);

        files.forEach((f) => {
            gapi.client.drive.files
                .update({
                    addParents: to,
                    removeParents: from,
                    fileId: file,
                })
                .then((res) => {
                    console.log(funcname, `Moved File: ${file} from ${from} to ${to}.`);
                    return res.result;
                }).catch(err => console.error(err));
        });
    }
}

Is there more information I need to fix this?


Solution

  • Working Final Example

    function folderSelectedCallback(folderData, fileData) {
        if (folderData.action === 'picked') {
            console.log('folderSelectedCallback(): called.');
            console.log('File Data: ', fileData);
            console.log('Folder Data: ', folderData);
            const files = fileData.docs;
            const folder = folderData.docs[0];
    
            console.log(files);
            console.log(folder);
    
            files.forEach((f) => {
    
                const options = {
                    addParents: folder.id,
                    removeParents: (typeof f.parentId !== 'undefined') ? f.parentId : '',
                    fileId: f.id
                };
    
                gapi.client
                    .request({
                        path: `drive/v3/files/${options.fileId}`,
                        method: 'PATCH',
                        params: options,
                        body: options
                    })
                    .then((response) => console.log(response))
                    .catch((err) => console.error(err));
            });
        }
    }
    

    For some reason using gapi.client.request in this manner works correctly.