未捕获的TypeError:无法读取未定义的属性“注释”

时间:2015-02-19 22:22:48

标签: javascript google-drive-api

我正在创建Google Drive Api网站以获取Google文档的评论。我每次提出请求时都会得到:未捕获的TypeError:无法读取属性'评论'未定义的。这是我的代码:

<Html>
<head>
    <title>test</title>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script src="https://apis.google.com/js/client.js"></script>
    <script>
        function auth() {
            var config = {
                'client_id': '<YOUR_CLIENT_ID>',
                'scope': 'https://www.googleapis.com/auth/urlshortener'
            };
            gapi.auth.authorize(config, function () {
                console.log('login complete');
                console.log(gapi.auth.getToken());
        });

        retrieveComments();


    }



    function retrieveComments() {

        printComment('<fileid>');
    }
    function printComment(fileId) {
        var request = gapi.client.drive.comments.list({
            'fileId': fileId
        });
        request.execute(function (resp) {
            if (!resp.error) {
                console.log('Modified Date: ' + resp.modifiedDate);


                console.log('Content: ' + resp.content);
            } else {
                console.log('Error code: ' + resp.error.code);
                console.log('Error message: ' + resp.error.message);
                // More error information can be retrieved with resp.error.errors.
            }

        });
    }


</script>



</head>
<body>
    <button onclick="auth();">Authorize</button>
</body>
</Html>    

请帮忙!我准备把头发拉出来。

1 个答案:

答案 0 :(得分:0)

我发现了代码的问题。我授权脚本但从未调用过gapi.client.load('drive','v2',Callback)

<html>
<head>
<title>test</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script src="https://apis.google.com/js/client.js"></script>
<script>
    function auth() {
        var config = {
            'client_id': '<YOUR_CLIENT_ID>',
            'scope': 'https://www.googleapis.com/auth/urlshortener'
        };
        gapi.auth.authorize(config, function () {
            console.log('login complete');
            console.log(gapi.auth.getToken());
    });

        gapi.client.load('drive', 'v2', retrieveComments());




}

function retrieveComments() {

    printComment('<fileid>');
}
function printComment(fileId) {
    var request = gapi.client.drive.comments.list({
        'fileId': fileId
    });
    request.execute(function (resp) {
        if (!resp.error) {
            console.log('Modified Date: ' + resp.modifiedDate);


            console.log('Content: ' + resp.content);
        } else {
            console.log('Error code: ' + resp.error.code);
            console.log('Error message: ' + resp.error.message);
            // More error information can be retrieved with      resp.error.errors.
        }

    });
}


</script>



</head>
<body>
    <button onclick="auth();">Authorize</button>
</body>
</html>    

现在正在运作!