Calling $.ajax on the server renders TypeError: ajax is not a function

I am trying to populate the database with some fake data using https://randomuser.me/api/
This is done in file “/server/fixtures.js” as you can see in the code below.
When I run meteor I get this message in command prompt on windows: TypeError: ajax is not a function

The same code if run on the client works perfectly fine with no errors and success function called.
This is done in file “/client/main.js” within a Meteor.startup function without importing $.

Just to be sure, I also tried those import syntaxes below and I still get the same error
import { $ } from ‘meteor/jquery’;
or also
import jQuery from ‘meteor/jquery’;
or even both

I also tried installing jQuery from npm. I still get the same error.

Anyone had this problem ?

// code in file "/server/fixtures.js" renders TypeError: ajax is not a function

import $ from 'meteor/jquery';

Meteor.startup ( () => {

  $.ajax({
    url: 'https://randomuser.me/api/?results=3',
    dataType: 'json',
    success: function(data) {
      console.log(data);
    }
  });
// code in file "/client/main.js" works fine with no errors

Meteor.startup ( () => {

  $.ajax({
    url: 'https://randomuser.me/api/?results=3',
    dataType: 'json',
    success: function(data) {
      console.log(data);
    }
  });

As far as I know, jQuery is not available server side. For example I wanted to use jQuery to scrap some webpages and I used cheerio to do that.It is a NPM package that acts as jQuery but server side.

Install the NPM package request and then: request.get() for the same behaviour. Remember to wrap it on Meteor.wrapAsync().

1 Like

Thanks. I found another package for async requests: axios from npm.

axios.get('http...').then( response => { /* do something with response */ } )

Meteor has its own package for this.

3 Likes