Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass params with redirect #1610

Open
DblK opened this issue Mar 28, 2024 · 10 comments
Open

Can't pass params with redirect #1610

DblK opened this issue Mar 28, 2024 · 10 comments

Comments

@DblK
Copy link

DblK commented Mar 28, 2024

Inside a router function, I tried to redirect to a login page but I which to keep the params.
I thought that I can do that:

this.redirect('NameOrPAth', null, { queryParams: { a: 42 } })

But it seems not working. I also tried, hash, params, query, randomThing, etc..
Is there a way to do so? Or should I compute back all params inside the path to make it work?

@ddaydd
Copy link

ddaydd commented Mar 28, 2024

why not use Router.go? I'm not sure this.redirect still exists...

Edit :
I just tested it, it works
this.redirect('name', { eventId: 'blabla' }, { query: 'q=s' });

@DblK
Copy link
Author

DblK commented Mar 28, 2024

Weird, on my Blaze onCreated/onRendered function I could not get it at all (with Router.current().params.query), either with go or redirect.
Whereas if I replace with a path with a manual ?q=s then the params can be retrieved.

How did you retrieve the params? The same way as me?

@ddaydd
Copy link

ddaydd commented Mar 28, 2024

Yes I retrieve the settings the same way as you.
Does your redirect work? your url ends up with /?q=s

@ddaydd
Copy link

ddaydd commented Mar 28, 2024

... Iron router will probably no longer be updated, if you start you should use FlowRouter-extra ...

@DblK
Copy link
Author

DblK commented Mar 28, 2024

Yes I retrieve the settings the same way as you. Does your redirect work? your url ends up with /?q=s

Yes my redirect is working but no query params in it... unless I manually add them to the url to redirect to.

It's not a start of a project, so can't switch to another router without a lot of work. Thanks for your time to help me.
I still have the workaround with the params, so I might go that way but does not feel right...

@ddaydd
Copy link

ddaydd commented Mar 28, 2024

can you share more code of your route?

@DblK
Copy link
Author

DblK commented Mar 28, 2024

router.ts

const route = (path: string, templateName: string, options?: routeOptionsType) => {
  Router.route(path, function (this: RouteController) {
    if (templateName === 'Home') {
      if (!Meteor.loggingIn() && !Meteor.user()) {
        // this.redirect(`/login`, null, {query: Router.current().params.query}); // Did not work...
        this.redirect(`/login?${(new URLSearchParams(Router.current().params.query)).toString()}`); // This is working
        return;
      }
    }
    this.render(templateName);
  }, { layoutTemplate: '', template: templateName, name: options?.routerTemplateName || templateName });
};
route('/dashboard', 'Home');
route('/login', 'Login');

login.ts (with login.hbs.html near to it)

import { Router } from 'meteor/iron:router';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';

import './login.hbs.html';

Template.Login.onCreated(() => {
console.log('Login', Router.current().params.query);
});

@ddaydd
Copy link

ddaydd commented Mar 28, 2024

and this ?
this.redirect(/login, null, {query: new URLSearchParams(Router.current().params.query)).toString()});

@DblK
Copy link
Author

DblK commented Mar 28, 2024

This is not working either.

@joshuaheine12345
Copy link

Okay we well work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants