Proper way to return res.redirect('')?


  • GNU/Linux

    I'm trying to write some redirection if a specific value is found then send error message from my plugin.

    Each time i seem to do the redirection code block

            winston.warn('Could not find value for user:'+uid);
            return res.redirect('/?template=fail');
    

    It works, but then restarts the whole forum.

    2019-05-26T04:50:05.304Z [4567/15619] - error: uncaughtException: Cannot set headers after they are sent to the client
    Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    

    Guess its some middleware issue calling directly from helpers like notAllowed seems to work


  • Staff Admin

    @Joykiller said in Proper way to return res.redirect('')?:

    Cannot set headers after they are sent to the client

    This usually means res.send or res.redirect was already called and you are calling it again. What is the rest of the code?


  • GNU/Linux

    @baris said in Proper way to return res.redirect('')?:

    @Joykiller said in Proper way to return res.redirect('')?:

    Cannot set headers after they are sent to the client

    This usually means res.send or res.redirect was already called and you are calling it again. What is the rest of the code?

    Probably would help huh, sorry about that.

    So I'm editing the Stripe Plugin for subs, which was based on the paypal system.
    Trying to add a new field to check before it goes to the onsubscription function.

    // get value
    stripe.isValueID = async function(uid, callback) {
      user.getUserField(uid, 'valueId', callback);
    };
    
    stripe.subscribe = async function(req, res, next) {
    var uid = req.user.uid;
    
    
    // this part breaks because of header shit.
    stripe.isValueID(uid, function(err, isValueID) {
      //console.log(isValueID);
      // if(!isValueID) {
        //return helpers.redirect('/?subscribe=fail');
        //return notAllowed(req, res);
        //return res.redirect('/?subscribe=fail');
      //}
    });
    
    

    https://github.com/edthefifth/nodebb-plugin-stripe-subscriptions/blob/master/lib/stripe.js
    around line 62. but each time i check the if conditional it triggers the sent headers issue.


  • Staff Admin

    You didn't post the rest of the stripe.subscribe function.


  • GNU/Linux

    @baris said in Proper way to return res.redirect('')?:

    You didn't post the rest of the stripe.subscribe function.

    Yeah thats why i listed stripe.js git, All i am doing is checking for value I created stripe.isValueID to call the valueId back and if its there or not pass res redirect. nothing else is changed just added that above code, //get value is above the stripe.subscribe block. It's fairly large section.

    I get it to return the valueId no problems and conditional works fine. Problem is trying to do the return res.redirect causes the header issue. Just not entirely sure why.


  • GNU/Linux Admin

    Callbacks don't work all that nicely with async functions... I think stripe.subscribe is called with await and returns undefined before the isValueID method is called and returns.


  • GNU/Linux

    @julian said in Proper way to return res.redirect('')?:

    Callbacks don't work all that nicely with async functions... I think stripe.subscribe is called with await and returns undefined before the isValueID method is called and returns.

    I added asyncs to test, removed them same issue with headers response from res.redirect. Oh i see what this plugin does, I guess i could write it to the session and then call it from there. Argh im at a loss that didnt work either still same header issue. seems like even the other values prob would do the same as mine I duno if the way it was coded from base was correct.


  • GNU/Linux

    @Joykiller said in Proper way to return res.redirect('')?:

    @julian said in Proper way to return res.redirect('')?:

    Callbacks don't work all that nicely with async functions... I think stripe.subscribe is called with await and returns undefined before the isValueID method is called and returns.

    I added asyncs to test, removed them same issue with headers response from res.redirect. Oh i see what this plugin does, I guess i could write it to the session and then call it from there. Argh im at a loss that didnt work either still same header issue. seems like even the other values prob would do the same as mine I duno if the way it was coded from base was correct.

    Nevermind I got it working with writing to the session first, had an error on previous attempt.

    Thanks for the help.


Log in to reply
 

Suggested Topics

  • 5
  • 3
  • 2
  • 1
  • 11
| |