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
- index.js
- 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