Using JQuery with Node.js "ReferenceError: $ is not defined"
Using JQuery with Node.js "ReferenceError: $ is not defined"
我正在尝试使用此框架 https://github.com/Schmoopiie/twitch-irc 在 Node.js 中为 Twitch 构建一个简单的 IRC 机器人,它可以连接到 API 并检查某些内容,在本参考资料中我将正在检查机器人所在的当前频道上的关注者。除了我 运行 遇到错误外,我一直在下面记录此错误。
[31mcrash[39m: ReferenceError: $ is not defined
at client.<anonymous> (C:\Users\Exuviax\Desktop\node.js\bot.js:157:9)
at client.emit (events.js:117:20)
at C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:657:30
at Object.createChannelUserData (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\data.js:56:2)
at client._handleMessage (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:651:22)
at Stream.emit (events.js:95:17)
at drain (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:36:16)
at Stream.stream.queue.stream.push (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:45:5)
at LineStream.<anonymous> (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\index.js:22:16)
at LineStream.emit (events.js:95:17)
这是我用来 运行 的代码,目的是检查 API
var irc = require('twitch-irc');
var colors = require('colors');
var jQuery = require('jQuery')
var jsdom = require("jsdom");
var window = jsdom.jsdom().parentWindow;
jsdom.jQueryify(window, "http://code.jquery.com/jquery-2.1.3.min.js", function () {
var $ = window.$;
$("body").prepend("<h1>The title</h1>");
console.log($("h1").html());
});
// Calling a new client..
var client = new irc.client({
options: {
debug: true,
debugIgnore: ['ping', 'chat', 'action'],
logging: true,
tc: 3
},
identity: {
username: 'BotName',
password: 'oauth:Code'
},
channels: ['#my', '#first', '#connect', '#channels']
});
// Connect the client to server..
client.connect();
client.addListener('chat', function(channel, user, message) {
if (message.indexOf('!followers') === 0) {
var channels = channel
channels = channels.replace('#', '');
$.getJSON('https://api.twitch.tv/kraken/channels/' + channels + '.json?callback=?', function(data) {
var followers = data.followers
client.say(channel, "Current Followers: " + followers);
console.log("Followers for " + channel + " " + followers);
});
}
});
几个小时以来,我一直试图让这个引用错误消失,但我对在浏览器环境之外使用 JS 和 JQ 来产生任何结果还不够熟悉,任何见解都将不胜感激。
Juhana Jan 在原问题的评论中提供了正确答案:
当您说 var jQuery = require('jQuery') 时,您是在将 jQuery 分配给名为 jQuery 的变量。如果您想改用 $,则必须执行 var $ = require('jQuery')。 – Juhana Jan
请务必使用 $
符号等于 require('jquery')
在 您将要使用的每个模块中 jQuery.
正确的语法是:
var $ = require("jquery");
有关详细信息,请访问 package。
我正在尝试使用此框架 https://github.com/Schmoopiie/twitch-irc 在 Node.js 中为 Twitch 构建一个简单的 IRC 机器人,它可以连接到 API 并检查某些内容,在本参考资料中我将正在检查机器人所在的当前频道上的关注者。除了我 运行 遇到错误外,我一直在下面记录此错误。
[31mcrash[39m: ReferenceError: $ is not defined
at client.<anonymous> (C:\Users\Exuviax\Desktop\node.js\bot.js:157:9)
at client.emit (events.js:117:20)
at C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:657:30
at Object.createChannelUserData (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\data.js:56:2)
at client._handleMessage (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:651:22)
at Stream.emit (events.js:95:17)
at drain (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:36:16)
at Stream.stream.queue.stream.push (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:45:5)
at LineStream.<anonymous> (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\index.js:22:16)
at LineStream.emit (events.js:95:17)
这是我用来 运行 的代码,目的是检查 API
var irc = require('twitch-irc');
var colors = require('colors');
var jQuery = require('jQuery')
var jsdom = require("jsdom");
var window = jsdom.jsdom().parentWindow;
jsdom.jQueryify(window, "http://code.jquery.com/jquery-2.1.3.min.js", function () {
var $ = window.$;
$("body").prepend("<h1>The title</h1>");
console.log($("h1").html());
});
// Calling a new client..
var client = new irc.client({
options: {
debug: true,
debugIgnore: ['ping', 'chat', 'action'],
logging: true,
tc: 3
},
identity: {
username: 'BotName',
password: 'oauth:Code'
},
channels: ['#my', '#first', '#connect', '#channels']
});
// Connect the client to server..
client.connect();
client.addListener('chat', function(channel, user, message) {
if (message.indexOf('!followers') === 0) {
var channels = channel
channels = channels.replace('#', '');
$.getJSON('https://api.twitch.tv/kraken/channels/' + channels + '.json?callback=?', function(data) {
var followers = data.followers
client.say(channel, "Current Followers: " + followers);
console.log("Followers for " + channel + " " + followers);
});
}
});
几个小时以来,我一直试图让这个引用错误消失,但我对在浏览器环境之外使用 JS 和 JQ 来产生任何结果还不够熟悉,任何见解都将不胜感激。
Juhana Jan 在原问题的评论中提供了正确答案:
当您说 var jQuery = require('jQuery') 时,您是在将 jQuery 分配给名为 jQuery 的变量。如果您想改用 $,则必须执行 var $ = require('jQuery')。 – Juhana Jan
请务必使用 $
符号等于 require('jquery')
在 您将要使用的每个模块中 jQuery.
正确的语法是:
var $ = require("jquery");
有关详细信息,请访问 package。