I have a feature in which locally saved content for anonymous users is saved to the db after a user signs in. After the local content has been inserted to the db, I want to redirect to the new post.
The challenge I am facing is how to get Iron Router to wait for the insert to complete before redirecting to the new post. The current version below works, but it feels clunky and I get the following warning from Iron Router because I am using Router.go rather than this.next(): Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?
What is a better way (than below) to wait for a db insert to complete before redirecting to the new item/post?
Triggering Event
User initiates login via 3rd party. (I am using vanilla Accounts-UI/LoginButtons)
First Before Hook: Check if there is local content
I have a session var that is set to true if locally stored content has been entered.
Router.onBeforeAction(function checkIfLocalContentOnLogin(){
if(Meteor.loggingIn() && Session.get("hasLocalContent")){
Session.set("startSaveToAcct", true);
}
this.next();
}
);
Second Before Hook: Runs after user is signed in and local content was found
Router.onBeforeAction(function saveToAcct(){
if(Meteor.userId() && Session.get("startSaveToAcct")){
var postAttributes = {
title: Session.get("docTitle"),
content: Session.get("localContent")
};
//I would like to wait for this to be ready before continuing.
Meteor.call('createPost', postAttributes, function(error, result){
if (error){
console.log("create post error: " + error.reason);
} else {
Session.setPersistent("localContent", "");
Session.set("docTitle", "");
Session.set("startSaveToAcct", false);
// I cannot call "this.redirect(...)" from in here, which is why I get the IR warning
Router.go('editPost', { _id: result._id });
}
});
//I would like to be able to call "this.redirect(...)" from here, but cannot do so because I can't get IR to wait for the above Meteor.call method (on the client, which is async) before continuing.
} else {
this.next();
};
});
Any suggestions re. a better way of doing this (eg without the IR warning, and maybe a bit less clunky) would be appreciated!