[英]Cannot call class as a function Javascript
I am building a simple stream to publish on PubNub and later consume. 我正在构建一个简单的流,以在PubNub上发布并稍后使用。 I can successfully utilize separate functions which log the output to the console or even create a .json file using the .pipe(new function())
. 我可以成功利用单独的函数将输出记录到控制台,甚至可以使用.pipe(new function())
创建.pipe(new function())
文件。 However for some reason with this function I am getting the Cannot call a class function. 但是由于某种原因,我得到了Cannot call an class function。 I have not changed the syntax of the function call at the end of the code, so I am assuming it is something in the PubNubStreamOut() function itself. 我没有在代码末尾更改函数调用的语法,因此我假设它是PubNubStreamOut()函数本身中的内容。
TypeError: Cannot call a class as a function at _classCallCheck (D:\\pasco\\node_modules\\pubnub\\lib\\node\\index.js:27:99) at _class (D:\\pasco\\node_modules\\pubnub\\lib\\node\\index.js:37:5) at new PubNubOutStream (D:\\pasco\\pubnub.js:24:13) at Object. TypeError:无法在_class(D:\\ pasco \\ node_modules \\ pubnub \\ lib \\ node \\ index处的_classCallCheck(D:\\ pasco \\ node_modules \\ pubnub \\ lib \\ node \\ index \\ js.27:99)中作为函数调用类.js:37:5),位于Object处的新PubNubOutStream(D:\\ pasco \\ pubnub.js:24:13)。 (D:\\pasco\\pubnub.js:54:42) at Module._compile (module.js:635:30) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10) (D:\\ pasco \\ pubnub.js:54:42)位于Module._compile(module.js:635:30)位于Object.Module._extensions..js(module.js:646:10)在Module.load(在function.Module.runMain(module.js:676:10)的tryModuleLoad(module.js:497:12)的tryModuleLoad(module.js:497:12)的module.js:554:32)
The .js file follows: .js文件如下:
var pubnub = require('pubnub');
var util = require('util');
var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var Twitter = require('twitter');
var pncfg = {
ssl : true, // enable TLS Tunneling over TCP
publish_key : "PUB_KEY",
subscribe_key : "SUB_KEY"
};
var twcfg = {
consumer_key:"...",
consumer_secret:"...",
access_token_key:"...",
access_token_secret:"..."
}
function PubNubOutStream(cfg, channel) {
Writable.call(this,{objectMode:true});
var pn = pubnub(cfg);
this._write = function(obj, encoding, callback) {
pn.publish({
channel: channel,
message: obj,
callback: () => callback()
});
};
}
util.inherits(PubNubOutStream, Writable);
function TwitterStream(cfg, query) {
Readable.call(this,{objectMode:true});
var client = new Twitter(cfg);
this._read = function() { /* do nothing */ };
var self = this;
function connect() {
client.stream('statuses/filter', {track: query},
function(stream) {
stream.on('data', (tweet) => self.push(tweet));
stream.on('error', (error) => connect());
});
}
connect();
}
util.inherits(TwitterStream, Readable);
new TwitterStream(twcfg,"#twitter").pipe(new PubNubOutStream(pncfg,"awesome-tweets"));
I am not sure why I am getting the class as a function error since I am using the new PubNubOutStream()
我不确定为什么将类作为函数错误获取,因为我正在使用new PubNubOutStream()
According to PubNub's documentation , you should use the following syntax to create a new instance: 根据PubNub的文档 ,应使用以下语法创建新实例:
var PubNub = require('pubnub');
var pn = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
});
The error is due to the fact, that you are calling the PubNub constructor as a function: 该错误是由于您将PubNub构造函数作为函数调用而导致的:
var pn = pubnub(cfg);
And the error message just confirms that: 错误消息只是确认:
TypeError: Cannot call a class as a function at _classCallCheck TypeError:无法在_classCallCheck上将类作为函数调用
Hope this will put you back on track. 希望这会让您重回正轨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.