Gapi.client.drive.files.update()返回错误“Parse Error”

问题描述 投票:0回答:1

使用谷歌api,我必须使用基本相同的代码集,但产生不同的结果。

两者都只需要一个fileId和两个parentId,一个要删除,一个用google api添加。它本质上是谷歌驱动器中文件的移动操作。有效的代码,只需要一个(from, to, file) => {}的签名和一个不签名的签名传递给Google Picker Call的回调中的字段。 (Link to Picker API Docs

第一个返回成功,第二个获取来自api的响应,表明存在解析错误,除了可能导致解析错误的其他方面几乎没有。

Examples:

工作实例

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

非工作实例

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

我需要更多信息来解决这个问题吗?

javascript google-api google-drive-sdk google-picker
1个回答
0
投票

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

出于某种原因,以这种方式使用gapi.client.request正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.