question

newuser-2f7456e0-dc95-450e-93fb-7d927a7f4aec avatar image

Pass connection variable to another files

Hi,


I'm creating a skill that will do stuff on Salesforce platform. I'm using Jsforce library to do it.


Here is the case.

I've 2 files

  1. index.js
  2. restServices.js

I wanted to separate my rest endpoint actions into the second file, leaving index with only the responses after processing the request. I did account linking and it is working fine. Using below code I am establishing connection(post oAuth authentication on Alexa app) as explained here. and here is my code.


function onLaunch(launchRequest, session, response) {
    if (!session.user.accessToken) {
        response.speechText = "Hi there, to experience the best of our service, request you to link your account. please click on, Link Account in your alexa app";
        response.isAccountLinking = "true";
        response.shouldEndSession = true;
        response.done();
    } else {
        var conn = new jsforce.Connection({
            instanceUrl: process.env.INSTANCE_URL,
            accessToken: session.user.accessToken
        });
        conn.identity(function (err, res) {
            session.attributes.loggedInUser = res;
            if (err) { return console.error(err); }
            console.log("user ID: " + res.user_id);
            console.log("organization ID: " + res.organization_id);
            console.log("username: " + res.username);
            console.log("display name: " + res.display_name);
            response.speechText = `Hi ${res.display_name}, How can I help you today?`;
            response.repromptText = 'How can I help you today?';
            response.shouldEndSession = false;
            response.done();
        });
    }
}


This is working fine and giving the exact result as expected. Now I wanted to use this established session variable and pass to my restServices file, so that, I don't end up making unnecessary calls.


the way I'm doing it is before `conn.identity` block, I'm making a session variable as

session.attributes.conn = conn;



it is giving me error as

{    "errorType": "Error",    "errorMessage": "Unable to stringify response body",    "stack": [        "Error: Unable to stringify response body",        "    at _trySerializeResponse (/var/runtime/RAPIDClient.js:166:11)",        "    at RAPIDClient._post (/var/runtime/RAPIDClient.js:127:22)",        "    at RAPIDClient.postInvocationResponse (/var/runtime/RAPIDClient.js:39:10)",        "    at complete (/var/runtime/CallbackContext.js:34:12)",        "    at done (/var/runtime/CallbackContext.js:59:7)",        "    at succeed (/var/runtime/CallbackContext.js:63:5)",        "    at Object.succeed (/var/runtime/CallbackContext.js:105:16)",        "    at Response.done (/var/task/index.js:79:23)",        "    at /var/task/index.js:200:22",        "    at /var/task/node_modules/jsforce/lib/promise.js:72:9"    ] }


Thought some issue with stringify and changed this line to

session.attributes.conn = JSON.stringify(conn);

Now, I get the below error.


{    "errorType": "TypeError",    "errorMessage": "Converting circular structure to JSON",    "stack": [        "TypeError: Converting circular structure to JSON",        "    at JSON.stringify (<anonymous>)",        "    at /var/task/index.js:198:44",        "    at /var/task/node_modules/jsforce/lib/promise.js:72:9",        "    at process._tickCallback (internal/process/next_tick.js:61:11)"    ] }


This is very confusing, unable to understand where am I going wrong. Please let me know on how to fix it.


And also I want to know on how I can send this conn to my restServices file


Thanks

alexa skills kitalexanode_jsnodejs
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Anand@Amazon avatar image
Anand@Amazon answered

Hi there,

Thanks for reaching out to us !

It's not very clear from provided code regarding conn variable which you are saving in session attributes but it looks like it contain circular structure.

To solve such type of error you can import Util module and use "inspect" to pass circular structure which you can either console.log or stringify.

Check this link to know more about util.inspect(object[, options])

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-2f7456e0-dc95-450e-93fb-7d927a7f4aec avatar image
newuser-2f7456e0-dc95-450e-93fb-7d927a7f4aec answered

Hey @Anand@Amazon,


Thanks for the response :-). Here is my Issue in other words.


I want to add conn to session variables so that I don't end up making multiple calls after authentication is done, and also pass conn object from one file and to another file.


Thanks

1 comment
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Anand@Amazon avatar image Anand@Amazon ♦ commented ·

You can try saving session data into DynamoDB which you can later retrieve in other file.

0 Likes 0 ·