0
votes

This Meteor app has the insecure and autopublish removed and accounts-password added.
meteor:PRIMARY> db.users.find({}); also shows the presence of the only user credentials I used.
Invoking Meteor.call('addTasks1',params); throws the error, further checks show Meteor.userId() being null Why is that and how to fix it? Thanks

update As per the suggested fix by Stephen Woods;
When I change the method addTasks1 on the server from Meteor.userId() to this.userId, It still throws the error.

///////////////////////////
//     both/both.js      //
///////////////////////////
Tasks1 = new Mongo.Collection('tasks1');


///////////////////////////
//   client/client.js    //
///////////////////////////
Template.login.events({
  'click #logMe': function() {
     var credentials = [$('#id').val(), $('#pin').val()];
     Meteor.call('logMeIn', credentials);
   }
 });

Template.footer.events({
  'click button': function () {
    if ( this.text === "SUBMIT" ) {
      var inputs = document.getElementsByTagName('input');
      for (var i = 0; i < inputs.length; i++) {
        var params = {};
        params[inputs[i].name] = inputs[i].value;
        Meteor.call('addTasks1', params);
      }
    }
  }
});


///////////////////////////
//    server/server.js   //
///////////////////////////
Meteor.methods({
  logMeIn: function (credentials) {
     Accounts.createUser({username: credentials[0], password: credentials[1]});
  }
});

Meteor.publish('tasks1', function(){
  return Tasks1.find({userId: this.userId});
});

Meteor.methods({
  addTasks1: function (doc) {
    if (Meteor.userId()) {
      Tasks1.insert(doc);
    } else {
      throw new Meteor.Error("Not Authorized");
    }
  }
});
1
If you type Meteor.userId() into the console client-side does it say null? - Stephen Woods
Yes it does that when I type it in the browser console. - Fred J.
Are you actually logging the user in? Your Meteor Method logMeIn is only creating a user, not signing it in. - Stephen Woods
The docs link says "or Accounts.createUser) is currently in progress." So I thought that the user is logged in when the Accounts.createUser is called. Hummm - Fred J.

1 Answers

0
votes

You need to log the user in, use Meteor.loginWithPassword(<USERNAME>, <PASSWORD>) on the client side to log a created user in.

Also, addTasks1, your Meteor method, is executing on the server. You want to use this.userId instead of Meteor.userId() on the server.