Home > Software design >  How do I access a variable passed in an object array from client side to server side in js/googlescr
How do I access a variable passed in an object array from client side to server side in js/googlescr

Time:06-15

New js-er here so appreciate the support. I am passing an object array, userInfo{}, which has the following contents :

{"userName":"bob","userID":12345,"userDetail":1} from a client-side JavaScript using google.script.run.userClicked(userInfo).

Have used JSON.stringify() in an alert box to make sure the object array being passed is in the above format and then on the server side I am able to use the data when writing to a googlesheet cell.

e.g. ss.getRange(2, userInfo.userID).setValue(userInfo.userName).

This works fine.

My issue is when I want to use userInfo data in the server side script as a parameter:

e.g. var newUserDetail = userInfo.userDetail 5; or if (userInfo.userDetail = 1) {perform action} etc.

The script does not respond. Do I need to transform the object array elements somehow ? Or use a different syntax to userInfo.userDetail ?

Have searched for on variants of how to access variables passed from client side to server side in js/gs' with no success. I am sure this is super-obvious but still learning - so all help appreciated.

CodePudding user response:

The issue might be the way you are writing the condition inside of your if statement. In javascript, to compare two values you should use either == or ===.

== is used to compare two values regardless of their types, for example console.log("22" == 22) will print true. While console.log("22" === 22) will print false.

You mention your if statement having the condition userInfo.userDetail = 1, while it should be userInfo.userDetail === 1

  • Related