Redirecting to item after creation

Hey guys,

How do I redirect to an item I just created in after my server method call? How do I pass in the ID to iron router from the newly created item?

Template.workoutList.events({
  'submit form': function(event) {
    event.preventDefault();

    var title = new Date();

    Meteor.call('createWorkout', title);
    

    Router.go('workout.show', {_id: 'id'});
  }
});

Thank you!

You have to use callback function:

    Meteor.call('createWorkout', item, function (error, result)
    {
      if (!error) {
       console.log('Item created with ID: ' + result);
      }
    });

On server side you have to return id.

Hope it helps!

Hey dancering!

How do I return the id on the server side? Tried the below:

'createExercise': function(workoutId, name, sets, reps, kgs) {
    Exercises.insert({
      workoutId: workoutId,
      name: name,
      kgs: kgs,
      reps: reps,
      sets: sets,
    });
    return workoutId;

However I get undefined in my Meteor.call

Thanks for helping

Here is what you are looking for:

// Method
createExercise: function(name, sets, reps, kgs) {
  return Exercises.insert({ name: name, kgs: kgs, reps: reps, sets: sets });
}

// Event
'submit form': function(event) {
  var name = ..., sets = ..., reps = ..., kgs = ...
  var id = Meteor.apply(
    'createExercise', 
    [name, sets, reps, kgs], 
    { returnStubValue: true }
  );
  Meteor.defer(function() {
    Router.go('workout.show', { _id: 'id' });
  });
  return false;
}

{ returnStubValue: true } tells Meteor to enable the stub’s return value instead of returning undefined.
Meteor.defer() is usually required around Router.go() in an event handler (don’t ask me why).

Hi reached,

the function on server that inserts on collection returns the id

var id = Exercises.insert({...});
return id;
1 Like

Hi Steve,

I can’t seem to get your method to work. Shouldn’t there be a meteor call in there somewhere?

Hey dancering,

It worked using the callback :).

Thank you both so much!

Notice that using a callback defeats the latency compensation Meteor mechanism: your app waits for a full server round-trip before user sees something happening on the screen.

1 Like

But that is not a huge problem since this is “only” a redirect i assume?

The question is: do you want the redirect to be triggered immediately, or do you prefer to wait for the full server round-trip to complete before triggering it?

Hi,

This post helped me a lot to construct my callback, but the if give me nothing. my console.log doesnt displayed. I working with meteor-angular, bu the idea is is the same.

Client :

$scope.createEvent = function(name, email){
$meteor.call(‘saveEvent’, name, email, function (error, result){
if (error){
console.log(“error”);
} else {
console.log(“id :” + result);
}
});

  };

And my Methods :

saveEvent : function (nameEvent, emailEvent){
var NewEvent = Events.insert({
CreatedAt : new Date(),
nameevent: nameEvent,
emailevent: emailEvent
});
console.log(‘server:’ + NewEvent);
return NewEvent;

}

any idea ?

Thanks in advance !

I don’t see anything wrong in your code.

Thanks for your time steve, me neither ^^.
But it is like my code from the if is totally ignored nothing display in the console…

Hi,

Juste to let you know that I found a solution, but without using a Method. Maybe you will have an idea to make it work with a method, but for the moment here is my solution :smile:

$scope.createEvent = function(name, email){  
     
  				var NewEvent = Events.insert({
          CreatedAt : new Date(),
          nameevent: name,
          emailevent: email
        });
          console.log("id :" + NewEvent);
  				$state.go('organizer', { "eventId": NewEvent});	
    

  	};