google drive api init method changes data

93 Views Asked by At

Strange thing found:

var settings = {
            apiKey:"[api key]",
            client_id:"XXX-59qgl58th06ft9s160cnp28j7a3uunte.apps.googleusercontent.com",
            discoveryDocs:["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"],
            folder_id:"0B3vR4cBcxn4odVNTa0VjSmNab3M",
            redirect_uris:"http://localhost:8000",
            scope:"https://www.googleapis.com/auth/drive"
        };

        console.log(settings); //checked before init

            gapi.load('client:auth2', function(){
            gapi.client.init(
                settings
            );
            console.log(settings); //after init

After gapi inits, it changes settings value to:

client_id:"XXXX-59qgl58th06ft9s160cnp28j7a3uunte.apps.googleusercontent.com"
cookie_policy:"single_host_origin"
scope:"https://www.googleapis.com/auth/drive"

Cannot get why?! it only should get the settings and continue. Thanks

1

There are 1 best solutions below

2
On

Your settings variable is of type object and passing it inside gapi.client.init() you are passing it as a reference allowing gapi to access your settings variable directly. If you wish to keep your settings variable untouched, pass a clone of the same to init function.

 gapi.client.init(
     JSON.parse(JSON.stringify(settings));
 );

By performing JSON.stringify you convert the entire object into string which and again parsing it into an object, which creates a clone of your object which no longer has any reference to your variable.