Not able to use data returned by API

Hi Everyone,

I am trying to bind data returned by an API to a cookie and pass it to other pages but I am getting undefined in the cookie value instead.

I set up a cookie that holds two variables: the email, and the access token returned by the API on success event of a login form.
I set up the same cookie variables to get passed to another page, and then I attached those values to a title to see what their values are. As a result, for the email cookie, I am getting the right value, however for the token cookie I am getting undefined,

And I don’t know why? Any idea on why this might be happening?

How exactly are you trying to this?
Also you say one of the values works correctly and the other not? Are you sure the value which is not working is even correctly returned by your API?

I set up 2 variables in the cookie manager at the login page, the first is email, and the second is token.

When user logs in successfully, the login API returns the following:

{
"userId": 3,
"displayName": "[user name here]",
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzaWQiOiIwOGQ4MmVlNGRhNjEwNDExYjNjYzdlMWY0ODVmYzJlNyIsInJvbGUiOlsiUmVnaXN0ZXJlZCBVc2VycyIsIlN1YnNjcmliZXJzIiwiVXNlcnMiXSwiaXNzIjoicGxhbnRhbmFwcC1tZWRpY29udHJvbC5hcHBzLnBsYW50YW5hcHAuY29tIiwiZXhwIjoxNTk1NDk3NTcyLCJuYmYiOjE1OTU0OTM2NzJ9.xCccHAR37pRnESRsmwayDVa0kcFbG5m-Ltu4mfgAeIw",
"renewalToken": "o1gWnayGGctCBCGQYYcFvIZYrbkR1JiyUxvWEQUZc2NmyGczldEovRhGI0HYUcDV"

}

I want to pass the accesstoken value to other pages, because this token will be used as an input to other APIs that will display the data based on user access that is defined.

On successful server connect event, I created 3 events, set the email cookie value to the email input field, set the token cookie value to the access token returned by the login API, and redirect user to dashboard page.

<dmx-serverconnect id="sc_login" url="api/User/login" noload="noload"
	dmx-on:success="cookies1.set('token',sc_login.data.apiSecurity.data.accessToken,{});cookies1.set('email',email.value,{});text1.setValue(sc_login.data.apiSecurity.data.accessToken)">

On dashboard page, I created the variables for the cookies with the same names, and then in the title of the page, I inserted the cookie values of email, and token in order to see if they return the right values, as a result the email cookie works, but the token cookie returns undefined.

<p dmx-text="cookies1.data.email+'&nbsp;'+cookies1.data.token">

Just tested this locally and it appears to be working fine.
Can you check the browser dev tools, under Application > Cookies please? Do you see your cookie there and is there a value assigned to it?

Yes I see it there and the value for the token cookie is undefined. Email cookie is working fine.

image

How and when is this token returned actually? Are you sure it is there when the server action runs, or is it probably returned later?

The accesstoken gets returned by the api after a successful login event from the login form.

I tried to add a button with an onclick event to set the value of the token to the string returned from the API after successful login but still not working.

<button id="btn1" class="btn btn-primary" dmx-on:click="cookies1.set('token',sc_login.data.apiSecurity.data.accessToken,{})">Button</button>

Could this be a bug?

But i don’t see form in your example. I see only a server action:

<dmx-serverconnect id="sc_login" url="api/User/login" noload="noload"
	dmx-on:success="cookies1.set('token',sc_login.data.apiSecurity.data.accessToken,{});cookies1.set('email',email.value,{});text1.setValue(sc_login.data.apiSecurity.data.accessToken)">

Are you sure you are using a form? Sorry - it’s not really clear what and how are you doing and your explanation is a bit confusing …

1 Like

I just saw that I was binding the data to the cookie from the wrong data source :grimacing:

I mistakenly was binding the data to another server connect action that I wasn’t using, instead of binding it to the results returned from the form data.

Spent so many hours on this little mistake :pensive:

1 Like