job.log function doesn't run

Post Reply
MY001
Newbie
Posts: 9
Joined: Mon Feb 15, 2021 9:47 pm

job.log function doesn't run

Post by MY001 »

I am setting up a flow that connects to Google sheet to check if there are any inventory available for the incoming job or not.
I successfully connected to Google sheet and the script does what it suppose to do but the issue is that when I put job.log() function in the script but Switch skips them. Check the code below

Code: Select all

async function jobArrived(s,flowElement,job){

checkGs("Product Name",job);
    
}

async function checkGs(productName,job){
    const {google} = require('googleapis');
    const spreadsheetId = 'xxxxxxxxx00000xxxxxxx';
    const client = new google.auth.JWT(
        "test@xxx.iam.gserviceaccount.com",
        null,
        "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n",
        ['https://www.googleapis.com/auth/spreadsheets'],
    );
    const Sheets =  google.sheets({version:'v4' , auth:client});
    //Get all Rows
    
    Sheets.spreadsheets.values.get({
        spreadsheetId: spreadsheetId,
        range: "test!A:B"
    }, (err, result) => {
        if (err) {
            console.log(err);
    } else {
            job.log(LogLevel.Warning, 'I am here 1 '); //<< Switch skips this and doesn't run it
            source = result.data.values;
            const input = source.map(function (row, index) {
                row.unshift(index);
                return row;
            }).filter(function (iRow) {
                    return iRow[1] === productName;
                });
            var index = parseInt(input[0]) + 1; 
            input[0].shift(); 
                if (input[0][1] < 1) {
                    job.log(LogLevel.Warning, 'Not available in the inventory')//<< Switch skips this and doesn't run it

                }else{
            input[0][1] = input[0][1] - 1;
            let values = [
                input[0]
            ];
            const resource = {
                values
            };
            Sheets.spreadsheets.values.update({
                spreadsheetId: spreadsheetId,
                range: "test!A" + index + ":B" + index, 
                valueInputOption: "RAW",
                resource : resource
            }, (err, result) => {
                if (err) {
                    console.log(err);
                } else {
                    job.log(LogLevel.Warning, 'Found in inventory')//<< Switch skips this and doesn't run it
                }
            })
                };

        }
        
    });

}   
What I don't understand is that when I put the job.log() function outside the function of (Sheets.spreadsheets.values.get({}) it works.

By the way the function that connects to Google sheet and do the checkup is being executed but it skips the job.log() functions :(

Any idea for the solution?
patej
Member
Posts: 79
Joined: Sun Nov 25, 2012 12:15 pm

Re: job.log function doesn't run

Post by patej »

Hi,

You should use await:

Code: Select all

await job.log(LogLevel.Warning, 'Found in inventory')
and earlier, before 2020 Fall, the parameter array was required so it should be included if you're using an older Switch:

Code: Select all

await job.log(LogLevel.Warning, 'Found in inventory', [])
I hope it helps.
MY001
Newbie
Posts: 9
Joined: Mon Feb 15, 2021 9:47 pm

Re: job.log function doesn't run

Post by MY001 »

Thank you for your reply.

I tried that but still doesn't work.

I made functions of (err,result) as async so I could use await for the job.log() function but unfortunately the issue still same.

I think the parameter job doesn't access in the function of Sheets.spreadsheets.values.get({)}
patej
Member
Posts: 79
Joined: Sun Nov 25, 2012 12:15 pm

Re: job.log function doesn't run

Post by patej »

Oh, ok, yes that might be it. Can you log in the beginning of checkGs(), before Sheets.spreadsheets.values.get()? Have you tried running the script in debug mode (https://www.enfocus.com/manuals/Develop ... gging.html) so that you can see if job is available? Does Switch log messages tell you anything?
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: job.log function doesn't run

Post by freddyp »

checkGs is declared as an async function (which is correct) but whenever you call an asynchronous function you have to await it, so the line in jobArrived is wrong.

Apart from the fact that you have to await job.log you are using it in a callback function and inside that function the job variable is not available. I avoid the use of callback functions because I find them more difficult to read and because they can easily lead to the so-called callback hell. So I am not sure it will work, but you could try adding the job variable as a third parameter to the callback function.

In the code you have shared you are not sending the job to output, so even if you get the script to fetch the correct value from Google Sheets the input job will simply stay in the input folder.
MY001
Newbie
Posts: 9
Joined: Mon Feb 15, 2021 9:47 pm

Re: job.log function doesn't run

Post by MY001 »

Thank you @patej for the suggestion.

@freddyp Thank you for your feedback.
In regards to await in jobArrived function yes i forgot to write it due to rush, and I also aware of the script doesn't send job to an output. I was trying to debug that is why I used logs.

I've tried adding the job variable as 3rd parameter to the call back function but it didn't do anything.

What do you suggest to make it work?
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: job.log function doesn't run

Post by freddyp »

My suggestion is the as always (I am sure it is not the last time I will have to write this): work with promises, not with callbacks.

I do not know what package you are using, so I do not have any experience with it, but you could try this. Instead of writing

Code: Select all

Sheets.spreadsheets.values.get(
  {
    spreadsheetId: spreadsheetId,
    range: "test!A:B",
  },//here comes the anonymous callback function
  (err, result) => {
    if (err) {
      console.log(err);
    } else {
      job.log(LogLevel.Warning, "I am here 1 "); //<< Switch skips this and doesn't run it
      //...
    }
  }
);
await the function, put the result in a variable and use a try-catch block to handle errors:

Code: Select all

let values;
try {
  values = await Sheets.spreadsheets.values.get({
    spreadsheetId: spreadsheetId,
    range: "test!A:B",
  });
} catch (error) {
  //handle the error
}
//continue with the values
This is not guaranteed to work as is because packages aften have different functions for using them as promises Check the documentation.
Post Reply