I am trying to write a simple, portable function where I can pass in an object.property and set its value from the result. Simple example:
var myFunction = function (result){
var data;
//get some data
return result = data;
};
myObject.someData;
myFunction(pass reference to key of myObject.someData here)
I want to pass reference to key of myObject.someData here. Brownie points for also declaring it here instead of line above.
Now for my actual code:
var configurations;
var oldServer;
//make a call to get details of old server here
//helper function to stream json into a variable
var streamToString = function(stream, result) {
var data = '';
var chunk;
stream.on('readable', function() {
while ((chunk=this.read()) != null) {
data += chunk;
}
});
stream.on('end', function() {
data = JSON.parse(data);
return result = data;
});
}
//declare shadowsocks as a property, so it can be passed as a param
configurations.shadowsocks;
//
streamToString(sequest.get('root@'+ oldServer[0].networks.v4[0].ip_address, '/etc/shadowsocks-libev/config.json', {
privateKey: sshKey,
readyTimeout: 20000
}, function (error, stdout) {
if (error) {
throw error;
}
}), configurations.shadowsocks
);
I know I can do this the "wrong way" by doing:
stream.on('end', function() {
data = JSON.parse(data);
return configurations.shadowsocks = data;
});
}
but then it's not portable. I would like to make this portable.