Value is returned as 'null' in database on insert

Hey guys,

Im very new to Meteor, so please let me know if im doing something wrong, or if’s possible for me to improve on my code :smile: .

Im trying to insert a simple name value in my database. However the value is being returned as ‘null’ in my database, so maybe im not retrieving my value properly…

Can anyone point me in the right direction on this one?

Also is there a better meteor way of retrieving my value when the value is not coming from an input field?

My client side script:

'click .show': function(event) {
    event.preventDefault();

    var currentUserId = Meteor.userId();
    var element = event.target;

    var showName = $(element).text();

    Meteor.call('addShow');

    console.log(showName);

  }

My server insert method:

Meteor.methods({
    'addShow': function(showName) {
        var currentUserId = Meteor.userId();
        UsersShows.insert({
            name: showName,
            createdBy: currentUserId
        });
    }
});

My template:

<template name="show">
  <p class="show" name="{{name}}">
    {{name}}
  </p>
</template>

All help is appreciated as im still learning :smile:

Can you please provide the code you use to retrieve your data from UsersShows? (the find or findOne you use to get {{name}})

Yea ofc, my bad:

Server stuff:

UsersShows = new Meteor.Collection('usersShows');
Meteor.publish('showsSearch', function(query) {
  var self = this;
  try {
    var url = 'https://api.themoviedb.org/3/search/tv';
    var api_key = '?api_key=xxx';

    var response = HTTP.get(url + api_key + '&query=' + query);

    _.each(response.data.results, function(item) {
      var doc = {
        name: item.name

      };

      self.added('shows', Random.id(), doc);
    });

    self.ready();

  } catch(error) {
    console.log(error);
  }
});

Client:

Shows = new Meteor.Collection('shows');

Session.setDefault('searching', false);

Tracker.autorun(function() {
  if (Session.get('query')) {
    var searchHandle = Meteor.subscribe('showsSearch', Session.get('query'));
    Session.set('searching', ! searchHandle.ready());
  }
});

Template.body.events({
  'submit form': function(event, template) {
    event.preventDefault();
    var query = template.$('input[type=text]').val();
    if (query)
      Session.set('query', query);
  },
  'click .show': function(event) {

    var currentUserId = Meteor.userId();
    var element = event.target;

    var showName = $(element).text();

    Meteor.call('addShow');

    console.log(showName);

  }
});

Probably typos, just to be sure:

  • I see two collections: one named ‘userShows’ and the other one named ‘shows’. Is this what you mean to have?
  • Argument to Meteor.call(‘addShow’) is missing
    Can you please provide the client-side code with the ‘find’ or ‘findOne’ that gets you {{name}}?
1 Like

Im probably tackling this the wrong way, but I want to have a collection of shows that the user can select from (which is the ones im fetching from themoviedb.org api). But I also want to enable the user to actually add shows to their list, and the only way I could see that possible was to make a new collection and store the usersShows there.

Here is the entire client code:

Shows = new Meteor.Collection('shows');

Session.setDefault('searching', false);

Tracker.autorun(function() {
  if (Session.get('query')) {
    var searchHandle = Meteor.subscribe('showsSearch', Session.get('query'));
    Session.set('searching', ! searchHandle.ready());
  }
});

Template.body.events({
  'submit form': function(event, template) {
    event.preventDefault();
    var query = template.$('input[type=text]').val();
    if (query)
      Session.set('query', query);
  },
  'click .show': function(event) {

    var currentUserId = Meteor.userId();
    var element = event.target;

    var showName = $(element).text();

    Meteor.call('addShow');

    console.log(showName);

  }
});

And thanks for helping btw, its much appreciated!

When you are making the Meteor method call, you need to provide the variable containing the name value as a parameter.

Meteor.call('addShow', showName);

Checkout the docs for reference purposes.

1 Like

Hey tsega,

Thank you very much, that solved the issue! :smile: Also thanks to steve.

I see that you also makes me aware of the missing argument to the Meteor.call here, thanks steve!