We created a custom app that need to store and retrieve external IDs of a third party. We use then this IDs on external event to link with Ticket.
We have an issue with one of data storage call.
It breaks without error message.
The same code works well in local using sdk cli.
Mainly it’s a function that will test if the data storage exist, if not (error) it will call a function to create it.
If it exist it will update the value to add the new ID in the list.
You can see in source code file that then the action is to retrieve the data storage named “tickets_docusign_all” and
if success => log step 4
if error => log step 2
But console stop at step 1 app.js:9.
Is there an issue in our code ? But this is working correctly in local and was working in prod few weeks ago.
Basing on the screenshot, the caller of the updateEnvelops function is returning post line 9 since the function is not having any callback. Can you please try adding callback as function parameter and call at the end of the function?
@ManiDeepak_Vandrangi thanks for your precision. @hchoura tried adding a callback in the call please see below screens.
He define callback function but we got a log that function doesn’t exist
I would like to try providing a solution and make an attempt to solve your query. Since I don’t know the broader code, I will move forward with the details you’ve shared.
Problem as I understand
The execution context of saveEnvelopeToTicket(..) has client.data.get() and client.db.get(..) statements.
client.db.get(..)is a Promise.
If client.db.get(..) either resolves or rejects (in other words, if the key exists or doesn’t exist in the db), updateEnvelopsDocusignAll(..) is invoked.
In the execution context of updateEnvelopsDocusignAll(..) only [1] is logged to the console.
The developer expects within updateEnvelopsDocusignAll(..) function, client.db.get(..) to be invoked. An upon resolving, either success operation or the failure operation should be executed.
So what happens to other steps? [2], [4], [5], [6]
My assumption is that, when the JS thread is in saveEnvelopeToTicket(..) execution context,
client.db.get('ticket_docusign:'+id) is a promise. When JS thread of execution invokes a promise, two things happen
While the promise is resolving, the JS thread executes remaining JS statements in saveEnvelopeToTicket(..) 's execution context. The `success function on promise resolve is queued by JS (in a microtask queue).
However, there are no further statements after client.db.get('ticket_docusign:'+id)
The success function starts being executed by JS thread. Eventually updateEnvelopsDocusignAll(..) is invoked.
JS looks in outer scope for updateEnvelopsDocusignAll(..) function and finds it. Its execution precisely has only two JS statements.
function updateEnvelopsDocusignAll(..) {
console.log('[1] - add an envelope id in repository')
client.db.get("tickets_docusign_all").then(function success(resolvedData) {
// [4] - display current envelope list
// [5] - add envelope in list
}, function fail(error) {
// [2] - initiate storage
})
}
An console.log() to log [1]
client.db.get("tickets_docusign_all") JS statement. Again it’s a promise. So JS thread of exectuation will continue to run existing code in updateEnvelopsDocusignAll(..) execution context. Meanwhile success function of client.db.get(..) will be queued. JS thread of execution returns undefined
So only #5 statement executes and logs [1] to the console. You will have to tell the JS thread of execution to wait for client.db.get() either resolves or rejects in order to invoke success or fail functions respectively.
So,
function updateEnvelopsDocusignAll(..) {
console.log('[1] - add an envelope id in repository')
return new Promise((resolve, reject) => {
client.db.get("tickets_docusign_all").then(function success(resolvedData) {
// [4] - display current envelope list
// [5] - add envelope in list
resolve('display and add envelope complete')
}, function fail(error) {
// [2] - initiate storage
reject('initate storate done')
})
});
}
This way in saveEnvelopeTicket(..), JS thread of execution when executes the queued success/fail function of client.db.get('ticket_docusing:'+id), it knows updateEnvelopsDocusignAll(..) is a promise and waits to be resolved.
Let me know if that helped you make some progress. If yes, we will discuss, the following…
I am still struggling to understand how did this work locally in the first place and not in production. This might require me to have more context about the codebase. But we will visit this once you see some success with above suggestions.
@Saif we had a call with someone from Fresh to review the issue.
The assumption was based on the data storage that could exceed 8KB but this still doesn’t explain why we can’t get in data or error function.
We changed our approach to use a unique key of 24 caracters and this is still not working.
Last log come from line 14.
I read the above statement as problem is solved yet finding the cause of problem occurred in the first place. So I shifted focus on limits of Data Storage might as well be an cause.
but now I understand your problem persists.
I will reply to the on the private message that we were discussing. Let’s see if we can find a sample code or the either get on a call to solve this further using office hours.
We had two JS functions invoked one by one in the same execution context – ABC(..) and XYZ(..)
Both ABC(..) and XYZ(..) had methods when invoked will return promises.
ABC(..) has client.db.get(..) which can be either fulfilled F1 or rejectedR1.
XYZ(..) returns a promise to either fulfill F2 or rejectedR2.
Javascript queues F1, F2 in a microtask queue. Both callbacks F1 and F2 will be invoked only when they are fulfilled. If JS encounters R1 during execution, JS will realize promise is rejected, and the thread of execution will move to the nearest .catch(..).
In the current situation, app has expected ABC(..) -> XYZ(..) -> R1 -> F2 to happen. But JS as it should, ABC(..) -> XYZ(..) -> R1 -> .catch(..) and exits. The catch wasn’t defined so JS silently exits.
We solved this problem forcing ABC(..) -> XYZ(..) -> F1 -> F2 -> if err -> .catch(..) to happen.
Here, ABC(..) is updateEnvelopTicket(..) XYZ(..) is updateEnvelopsDocusignAll(..)
Here is the sample we shared with the developer to do #7,
This topic will automatically close in 3 days.
For any new questions we request to either create a new topic
If you are this topic creator – we already have private thread where you can ask further questions.