Can not retrieving session after location.href

Hi experts, i have a problem with session after i ran command -> meteor add http and called my api.

Below is my code in login.js, after i got success response it should redirect to Calendar template.

HTTP.call("GET", 'http://........../WS_LDAP/customers/'+userVar+'/'+passVar
        , function( error, response ) {
          if ( JSON.parse(response.content.toString())=="true" ) {
            HTTP.call("GET", 'http://......../WS_LDAP/customers/'+userVar
            , function( error, response ) {
              if ( response.content!=null ) {
                Session.set("pic", userVar);
                Session.set("usernameaut", JSON.parse(response.content)[0].nama);

                location.href="/Calendar/"+userVar
              } 
              else if (response.content==null){
                alert("You are not authorized");
              }
               else{
                alert(error);
               } 
            })
          } else if (JSON.parse(response.content.toString())=="false"){
            alert("Please check you username or password");
          }
           else{
            alert(error);
          } 
      })

And here is Calendar.js for calling the session (i need session.get(“usernameaut”) as return from greeting function). To do that, i am including these in the Calendar.js

Template.Calendar_Page.helpers({
  greeting: function() {
    return Session.get('usernameaut');
  }
})

and in Calendar.html

<template name="Calendar_Page">

  <div class="ui raised segment container">
      <div id="a">Welcome {{greeting}}</div>
   <!--Add the events calendar to the template.-->
    <div id="event-calendar"></div>
    
  </div>

  <!-- Create the "create-event-modal" -->
  <div id="create-event-modal" class="ui modal">
    {{>Create_Event_Modal}}
  </div>

</template>

Session always return undefined. How can i fix this problem? thank you so much for your help

what router do you use?

I’m almost certain that location.href does a page load - which will destroy your Session variables.

4 Likes

Here it is @minhna


import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';

FlowRouter.route('/', {
  name: 'App_Login',
  action() {
    BlazeLayout.render('App_Layout', { main: 'App_Login' });
  },
});

FlowRouter.route('/Calendar/:name', {
  name: 'Calendar_Page',
  action(params) {
    BlazeLayout.render('App_Layout', { main: 'Calendar_Page' });
  },
});

FlowRouter.notFound = {
  action() {
    BlazeLayout.render('App_Layout', { main: 'App_Not_Found' });
  },
};

Actually it happened after i ran this command in cmd prompt

meteor add http.

Before the command execution, it ran well.

change this:

location.href="/Calendar/"+userVar

to this:

FlowRouter.go(`/Calendar/${userVar}`);
2 Likes

I am getting another error related to CORS. :smiley: , today it happen suddenly even though yesterday it was ok.

I can get a result from web api, it always return undefined at response.content

i think it because i ran the apps on the client side. I still don’t understand how to add content origin in meteor

You must add that header value on the server API, not on the client (meteor).
You can also use meteor Method run on server only to get the content from remote API.

i am testing the API using postman, and herewith the results:

.

I think i already added header value in the API.

hi @robfallows, thank you for answering. I changed it to flow.router as @minhna has wrote.