Node.js global variables not working - node.js

I want to use global variables in my project, but they don't work and I don't understand why they don't work. I'm trying to use them in the following way:
global.connection=null;
function create_connection(connection) {
connection=12345;
}
create_connection(global.connection);
console.log(global.connection); // returns null, why doesn't it return 12345?

Javascript always passes variables by value. So in your case you change the string value without keeping reference to global object.
You could have done this instead
function create_connection(global) {
global.connection=12345;
}
create_connection(global);

Related

Terraform variable to assign using function

variable "cidr" {
type = map(string)
default = {
development = "x.1.0.0/16"
qa = "x.1.0.0/16"
default = "x.1.0.0/16"
}
}
variable "network_address_space" {
default = lookup(var.cidr, var.environment_name,"default")
}
Am getting error that "Error: Function calls not allowed"
variable "subnet_address_space": cidr_subnet2_address_space = cidrsubnet(var.network_address_space,8,1)
A Terraform Input Variable is analogous to a function argument in a general-purpose programming language: its value comes from an expression in the calling module, not from the current module.
The default mechanism allows us to substitute a value for when the caller doesn't specify one, but because variables are intended for getting data into a module from the outside, it doesn't make sense to set the default to something from inside that module: that would cause the result to potentially be something the caller of the module could never actually specify, because they don't have access to the necessary data.
Terraform has another concept Local Values which are roughly analogous to a local variable within a function in a general-purpose programming language. These can draw from function results and other objects in the current module to produce their value, and so we can use input variables and local values together to provide fallback behaviors like you've illustrated in your question:
var "environment_name" {
type = string
}
var "environment_default_cidr_blocks" {
type = map(string)
default = {
development = "10.1.0.0/16"
qa = "10.2.0.0/16"
}
}
var "override_network_range" {
type = string
default = null # If not set by caller, will be null
}
locals {
subnet_cidr_block = (
var.override_network_range != null ?
var.override_network_range :
var.environment_default_cidr_blocks[var.environment_name]
)
}
Elsewhere in the module you can use local.subnet_cidr_block to refer to the final CIDR block selection, regardless of whether it was set explicitly by the caller or by lookup into the table of defaults.
When a module uses computation to make a decision like this, it is sometimes useful for the module to export its result as an Output Value so that the calling module can make use of it too, similar to how Terraform resources also export additional attributes recording decisions made by the provider or by the remote API:
output "subnet_cidr_block" {
value = local.subnet_cidr_block
}
As stated in Interpolate variables inside .tfvars to define another variable by the Hashicorp person, it is intended to be constant by design.
Input variables are constant values passed into the root module, and so they cannot contain interpolations or other expressions that do not yield a constant value.
We cannot use variables in backend either as in Using variables in terraform backend config block.
These are the things we Terraform users tripped on at some point, I suppose.

Groovy withBatch without need for closure

I am making use of withBatch API as:
int[] modifyCount = sql.withBatch(batchSize, updateQuery) { ps ->
keyValue.each { k,v ->
ps.addBatch(keyvalue:k, newvalue:v)
}
}
In closure, I set values for the placeholders in the updateQuery. It works fine.
Suppose updateQuery has already all the fields defined and hence code inside closure above is not actually needed now.
What change is needed for that?
there is no reason to use withBatch if updateQuery contains all values.
just use
sql.execute( updateQuery )
doc: http://docs.groovy-lang.org/latest/html/api/groovy/sql/Sql.html#execute(java.lang.String)

Export and Use same function in node js

I have exported a function to set random color, which returns me a random color for every call.
like,
exports.getRandomColor = function(index) {
//return random color
}
If I put function like this I can be able to get access to that function from another file. But not inside the same file. (i.e) This getRandomColor is not get accessed in the same file,
It works good if I use
function getRandomColor(index) {
//return random color
}
The problem is I cannot able to access this on another file. Suggestions are welcome.
Thanks in advance
So you can have the next in your file
// define named function
function getRandomColor(index) {
return random color
}
// assign it to the exports, like below
exports.getRandomColor = getRandomColor
Then you can reuse this function whenever you want.

Parse integer or provide default value with unwrap_or, but print error message when default value is used

I have written some code that parses a config file. If the config file holds a valid value for a field it sets in Config struct. If no valid integer value was found for a setting it sets a default value (e.g: 90).
let config = Config {
interval: settings.get("interval").unwrap().parse().unwrap_or(90),
}
How can I make this to take a closure, so that it can print via error! and set default value?
Looking something like following:
let config = Config {
interval: settings.get("interval").unwrap().parse().unwrap_or({
error!("No interval found. Using default: 90");
90
});
}
But in this example, the error! is always executed, even if a valid value from interval was read from the config.
How can I make it so that unwrap_or only executes the code in optb when parse() has failed?
How can I make it so that unwrap_or only executes the code in optb when parse() has failed?
Arguments passed to unwrap_or are eagerly evaluated.
If you are passing the result of a function call, it is recommended
to use unwrap_or_else which is lazily evaluated.
In your scenario it should be changed as following:
let config: Config = Config {
interval: settings.get("interval").unwrap().parse().unwrap_or_else(|_| {
error!("No interval found. Using default: 90");
90
}),
}
Playground
Also you should not use naked unwrap() in your production code.
Error handling is better solution instead of using naked unwrap()
Here you can find detailed info about why you should not use unwrap()

Can javascript function name contain a space?

I copied the code from kraken. I don't understand why there is a space between get and app(). Can someone please explain what's going on here?
var kraken = {
get app() {
return this._app;
},
use: function (route, delegate) {
//.....
}
}
No, in javascript a function cannot contain spaces. The code you are showing is using the get keyword to bind a property to a object.
get
Binds an object property to a function that will be called when that property is looked up.
Have a look to getters and setters in javascript.
It's a getter.
Check out this link.
The function is get and it's exposing a property called app.

Resources