This feature is nice, but it has been giving me a lot of problems too. I’m trying to implement the edit and update feature of a post. That all works fine, but it’s routing back to the single post page after the update is complete. Discover Meteor says to do it this way
Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('singlePost', {_id: currentPostId}); //here
}
});
},
This doesn’t work for my url arrangement. Here is the code I have so far, which seems like too much.
Lots of brackets! I get an error, but thankfully it updates and the app doesn’t crash
Exception in delivering result of invoking ‘/posts/update’: Error: Missing required parameters on path “/:userName/:_id”. The missing params are: [“userName”,"_id"]. The params object passed in was: {“path”:"/:_userName/:_currentPostId"}.
Update, I’m not getting this error with my current code
Exception in delivering result of invoking '/posts/update': Error: Missing required parameters on path "/:userName/:_id". The missing params are: ["userName","_id"]. The params object passed in was: "tyler/kisbMtGMCy6baQKaQ"
with the current code
Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('singlePost', user + '/' + currentPostId); //here
}
});
},
This is not JS-specific but rather required by iron:router because of how you defined your route:
path:'/:_userName/:_currentPostId',
This means that your path will expect both _userName and _currentPostId to be defined whenever it is called or printed as a string, so that’s true for the Router.go() call. If you leave one of those params out, then how should the route know what to put in place of the placeholders. That’s why the above solution worked. Not because of anything JavaScript-specific!
EDIT: And the correct invocation should actually be:
this.route('profile',{
path:'/:userName',
data:function(){
console.log(this.params); // now you see what you need to see
//return Profiles.findOne({userName: this.params.userName});
}
});
When in doubt use debugger or print stuff to console!
But yes, your last code snippets seem fine. If something is still not happening the way you intend it to, then
check the Database directly, using meteor mongo and something like db.profiles.findOne()
1a) or really make sure you publish and subscribe 100% correct and you can now just query everything on the client to see what’s there
check publications, check subscriptions
install msavin:mongol to easily check what’s there on the client
use console.log and debugger (meteor debug for server-side, Chrome dev tools etc for client-side) to see what values variables have exactly, and when.
The beauty of JS and Meteor is that you can poke at everything in console and the debugger. You can see all the data that’s there at any point and thus relatively easily begin understanding what’s actually happening and what different parts of the program are expecting you to do, what input you have to present to them, basically.