我安装了nodejs模块" googleapis" (npm install googleapis
)到Azure移动服务实例,我想用我在https://www.npmjs.org/package/googleapis找到的一些示例代码进行快速测试。尝试在google驱动器中创建文本文件的代码示例后,我在控制台中收到以下错误消息:
[TypeError: Cannot read property 'prototype' of undefined]
错误发生在当前版本的google-api-nodejs-client / apis / index.js(https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js)的第40行:
var Endpoint = require(__dirname + '/' + filename + '/' + path.basename(version));
调试时,我可以看到上述方法中使用的路径如下:
D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\apis\drive\v2
我已经确认路径正确。
以下是我试图运行的代码:
var google = require('googleapis');
var OAuth2Client = google.auth.OAuth2;
var CLIENT_ID = 'xxxx';
var CLIENT_SECRET = 'xxxxxx';
var REDIRECT_URL = 'xxxxx';
var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
oauth2Client.setCredentials({
access_token: identities.google.accessToken
});
var drive = google.drive({ version: 'v2', auth: oauth2Client });
drive.files.insert({
resource: {
title: 'Test',
mimeType: 'text/plain'
},
media: {
mimeType: 'text/plain',
body: 'Hello World'
}
}, function(err, response) {
console.log('error:', err, 'inserted:', response.id);
});
问题的根源是什么?
这是错误堆栈跟踪:
TypeError: Cannot read property 'prototype' of undefined at Object.<anonymous>
(D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\node_modules\multipart-stream\node_modules\sandwich-stream\lib\sandwich-stream.js:24:50)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\node_modules\multipart-stream\index.js:1:78)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
这是sandwich-stream.js的第24行:
SandwichStream.prototype = Object.create(Readable.prototype, {
constructor: SandwichStream
});
nodejs版本是:
Node version: v0.8.28
答案 0 :(得分:2)
我的猜测是,Azure移动服务正在使用__dirname做一些时髦的事情,这使得文件路径无法正确解析。你能查看运行它的日志吗?实际错误应该是console.log&#39; d来自此行:https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js#L45