Angular 2+ Project中的错误“无法读取未定义的属性'request'”

时间:2018-11-02 05:48:33

标签: javascript angular google-drive-api gapi

我正在编写代码,以便使用 gapi 将文本文件上传到Google驱动器中。我尝试使用下面的代码。

  var createTxtFile = function(name,text,parentId,token) {
    console.log("Inside  Code ");
    var auth_token = token;
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";
    const mimeType = 'text/plain';
    var contentType = text.type || 'application/octet-stream';
    var metadata = { 
      "name" : name,
      "mimeType": mimeType,
      "parents": [parentId]
    };  

    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        close_delim;
    console.log("After multipartRequestBody ;");
    var request = gapi.client.request({ 
        'path': '/upload/drive/v3/files/',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': { 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"', 'Authorization': 'Bearer ' + auth_token },
        'body': multipartRequestBody 
    });
    request.execute(function(file) { 
        console.log(file);
        console.log("Wrote to file " + file.name + " id: " + file.id); 
    });
}

但是,当我单击“提交”按钮时,直到控制台日志“在multipartRequestBody之后;” 之前,它都可以正常工作。然后,我得到以下错误

ERROR TypeError: Cannot read property 'request' of undefined
    at createTxtFile (VM5498 main.js:848)
    at ProfileComponent.push../src/app/profile/profile.component.ts.ProfileComponent.downloadcsv (VM5498 main.js:860)
    at VM5498 main.js:747

我尝试更新“ html-webpack-plugin” ,但是没有运气。

0 个答案:

没有答案