简体   繁体   中英

Meteor, $ defined on client but not on server

I'm working on a Meteor app. I can call $.get() perfectly fine in this client javascript I run on a button click. But this client javascript calls some server javascript (with Meteor.call() ) in which I use $.get() and see the error:

Exception while invoking method 'server_facebook_authentication' ReferenceError: $ is not defined

I am not super solid on how Meteor loads resources and libraries and whatnot. What is wrong and how can I fix this?

The current version of the built-in Meteor jquery package does not load any code on the server:

https://github.com/meteor/meteor/blob/devel/packages/jquery/package.js

jQuery 1.x is not designed to run server-side anyhow, although people have been using it anyway. 2.0 has support for server-side JS. Meteor is currently transitioning to a new packaging system, so we may well see jQuery 2.0 with server-side code available in the future.

For now though, you should use Meteor's HTTP library ( http://docs.meteor.com/#http ) which works on both server and client, and is likely to be much more reliable on the server-side than jQuery.

You can use meteor's HTTP library or you can simply require jQuery on the serverside:

var jQuery = require('./jquery.min.js');

jQuery.get() //this will now work.

It's been said but this is probably not what you want to do. You can use jQuery or Underscore (for example) for other methods and that is perfectly acceptable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM