Lab 2 Edge Developer: 2 Different javascript with shared variables

Not applicable

Hi there,

I've been attempting the learning videos provided by apigee. I'm currently stuck at the part where theres two javascript and the second one referenced some of the variables created at the first javascript. For example:

var ex1 = httpClient.get("http://www.example.com?api1"); 
context.session["ex1"]= ex1;

// Put the object into the session 

var ex2 = httpClient.get("http://www.example.com?api2");
context.session["ex2"]= ex2;

This is in the first request javascript policy

ex1.waitForComplete();

// Thread is paused until the response is returned or error or step time limit has been reached.


ex2.waitForComplete(100);

// Thread is paused for a maximum of 100 ms.

var ex1 = context.session["ex1"];


This is in the response javascript policy.

Apparently there's an error right at the first line , it complains that ex1 is not defined. Can i ask how do you go about referencing the variables from the first java script to the second ? The video doesnt state and it seems the http://docs.apigee.com/api-services/reference/javascript-object-model didnt explain clear on that either.

Nonetheless, thanks for the great resources again! Appreciates any help that can be put forward.

Regards,

Vincent

Solved Solved
1 5 121
1 ACCEPTED SOLUTION

@Vincent Oh -

Can you direct me to the source of this example code?

If the code you are showing me is accurate, then the problem is that it tries to reference ex1 before ex1 has taken a value. What I mean is. . . .

ex1.waitForComplete();          // use ex1 here, but it is not yet defined
ex2.waitForComplete(100);       // likewise ex2 is not defined; will fail similarly.
var ex1 = context.session["ex1"];  // first assign ex1 here

This isn't going to work!

I think you ought to be able to do something like this:

var ex1 = context.session["ex1"];  // get ex1 from session
ex1.waitForComplete();             // wait for the first exchange
var ex2 = context.session["ex2"];  // get ex2 from session
ex2.waitForComplete(100);          // wait for ex2, maximum of 100 ms.
...

But I'm not sure if that's going to work, without seeing the original source code.

View solution in original post

5 REPLIES 5

@Vincent Oh -

Can you direct me to the source of this example code?

If the code you are showing me is accurate, then the problem is that it tries to reference ex1 before ex1 has taken a value. What I mean is. . . .

ex1.waitForComplete();          // use ex1 here, but it is not yet defined
ex2.waitForComplete(100);       // likewise ex2 is not defined; will fail similarly.
var ex1 = context.session["ex1"];  // first assign ex1 here

This isn't going to work!

I think you ought to be able to do something like this:

var ex1 = context.session["ex1"];  // get ex1 from session
ex1.waitForComplete();             // wait for the first exchange
var ex2 = context.session["ex2"];  // get ex2 from session
ex2.waitForComplete(100);          // wait for ex2, maximum of 100 ms.
...

But I'm not sure if that's going to work, without seeing the original source code.

Hi Dino,

Thanks for the reply! The example is found here: http://docs.apigee.com/api-services/reference/javascript-object-model

The Specific example is at this screenshot:

2085-screen-shot-2016-03-02-at-64454-am.png

Notice at the second java script, the ex1.waitForComplete() was called before var ex1 = context.session["ex1"]. So it should be referencing the httpClient's ex1 defined earlier in the first javascript. I basically followed this entire screenshot. But the error again is saying that ex1 is not defined.

Thanks again!

ok, thanks for the link, I've fixed that.

In the meantime, you're problem is solved, correct? You have it working now, yes?

Yes it's fixed ! Thanks for the recitification and clarification!

Not applicable

Yes it's fixed . Thanks for the recitification and clarification!