Home > Enterprise >  Why is object key pair with null value not passed from server to client anymore?
Why is object key pair with null value not passed from server to client anymore?

Time:10-31

I have a Google application script web application where I use google.script.run.withSuccessHandler. Server side function returns an object where all the values are null. MaterializeCSS enter image description here

Is someone experiencing the same error? How to fix this?

CodePudding user response:

Issue:

If null is a value for a key in a object, the key-value pair is lost when the object is passed from server to client, though null is a legal parameter.I can confirm the issue.

Solution:

The issue is reported here. Add a star to the issue, if anyone else has the same issue.

As a typical workaround for illegal parameters, Use JSON.stringify() on the server side, pass the string to the client and JSON.parse() it client side to get nulls inside a object.

Server:

function returnObject(){
  return JSON.stringify({a:1,b:null,c:3});
}

Client:

document.addEventListener("DOMContentLoaded",    
 function(event) { 
  google.script.run
    .withSuccessHandler(afterDataReceived)
    .returnObject()
});

function afterDataReceived(receivedData){
  console.log(JSON.parse(receivedData));
}
  • Related