IRON-ROUTER; Route to path of post that was just created


#1

Hello. I need some help routing to the post i just created after cliking the submit button.

I create my posts with the following events helper.

 Template.submit.events({
"submit .newarguement": function(event) {
    var name = event.target.name.value;
    var arguement = event.target.arguement.value;
    var tag1 = event.target.tag1.value;
    var tag2 = event.target.tag2.value;
    var tag3 = event.target.tag3.value;
    var file = $('#productImage').get(0).files[0];
    if (file) {
        fsFile = new FS.File(file);
        ArguementCollection.insert(fsFile, function(err, result) {
            if (!err) {
                var productImage =
                    '/cfs/files/ArguementCollection/' +  result._id;
                Posts.insert({
                    name: name,
                    arguement: arguement,
                    tag1: tag1,
                    tag2: tag2,
                    tag3: tag3,
                    image: productImage,
                    username: Meteor.user().username,
                    userId: Meteor.userId()
                });
            }
        });
    } else {
        var productImage = '/img/empty.jpg';
        Posts.insert({
            name: name,
            arguement: arguement,
            tag1: tag1,
            tag2: tag2,
            tag3: tag3,
            image: productImage,
            username: Meteor.user().username,
            userId: Meteor.userId()
        });
    }

    event.target.arguement.value = "";
    event.target.tag1.value = "";
    event.target.tag2.value = "";
    event.target.tag3.value = "";
    
    Router.go('/');
    toastr.success("You just made a post!");
    return false;
}
});  

And as you can see, the route sends the post back to the homepage after submission. Each post however has its own page which is served by the route

this.route('onearguement', {
    path: '/arguement/:_id',
    data: function() { return Posts.findOne(this.params._id); }
});

I’d like to change the route in my events helper so that when the form is submitted , it immediatemy takes me to the individual page of the post that was just created.

Thanks .


#2

Add callback to Post.insert and redirect anywhere with post._id
But I’m prefer methods.


#3

I am still in development stage so I will not be using methods for a while. About the call backs, if the code isn’t too long can you please write it down for me? I am very new to Meteor.


#4
  Post.insert({...}, function(err, result) {
       if(!err) {
           Router.go('routename', {_id: result});
       }
  })

Of course, you have to create route routename for your post with _id param.


#5

my dear, its not working. can you please give me more. ive already created the route to the individual page and it works. but my event helper is so complex, i dont know how your code is going to fit in. ive tried its not working.


#6

And this is why I’m using methods (-:
OK. I think, the problem is what on client after Post.insert() you have fake _id right after insertion. When you receive the server response and your database is synced, the fake _id that was generated no longer exists, but Router.go() already tried to redirect to fake _id.
Maybe I’m wrong…
Use methods (-: