NodeJS-Google API模块-[TypeError:无法读取未定义的属性'prototype'] [英] NodeJS - Google API Module - [TypeError: Cannot read property 'prototype' of undefined]
问题描述
我将nodejs模块"googleapis"(npm install googleapis
)安装到Azure移动服务实例,并且我想使用在
I installed the nodejs module "googleapis" (npm install googleapis
) to an Azure Mobile Services instance and I wanted to do a quick test with some sample code I found here https://www.npmjs.org/package/googleapis. After trying the code sample which creates a text file in google drive I get the following error message in the console:
[TypeError: Cannot read property 'prototype' of undefined]
该错误发生在当前版本的google-api-nodejs-client/apis/index.js(
The error is occurring at line 40 of the current version of google-api-nodejs-client / apis / index.js (https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js):
var Endpoint = require(__dirname + '/' + filename + '/' + path.basename(version));
调试时,我可以看到上述方法中使用的路径如下:
When debugging I can see the path used in the above method as the following:
D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\apis\drive\v2
我已验证路径正确.
这是我尝试运行的代码:
Here is the code I tried to run:
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行:
Here is line 24 of sandwich-stream.js:
SandwichStream.prototype = Object.create(Readable.prototype, {
constructor: SandwichStream
});
nodejs版本为:
The nodejs version is:
Node version: v0.8.28
推荐答案
我的猜测是Azure移动服务使用__dirname做一些时髦的事情,导致文件路径无法正确解析.您可以检查运行日志的位置吗?实际错误应该是从以下行进行console.log记录: https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js#L45
My guess is that Azure Mobile Services is doing something funky with __dirname that is making the file path not resolve correctly. Can you check the logs where you are running this? The actual error should be console.log'd from this line: https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js#L45
这篇关于NodeJS-Google API模块-[TypeError:无法读取未定义的属性'prototype']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!