Having tslint warnings (ES6) and still having some difficulty with its syntax. It show as following
error no-shadowed-variable: Shadowed name: 'i'
for (let i = 0; i < this.state.UploadedFilesArray.length; i++) {
if (this.state.UploadedFilesArray[i] != undefined) {
var tempx = this.state.UploadedFilesArray[i].toString().split('|');
str.push(<li key={tempx[0]} onClick={this.onChangeDeleteDocument.bind(this)} data-id={tempx[1]}> Uploaded File : {tempx[0]} - <a className={styles.MyHeadingsAnchor}>Delete </a></li>);
}
}
Check out the no-shadowed-variable rule's documentation here: https://palantir.github.io/tslint/rules/no-shadowed-variable/
When a variable in a local scope and a variable in the containing scope have the same name, shadowing occurs. Shadowing makes it impossible to access the variable in the containing scope and obscures to what value an identifier actually refers.
It'd be helpful to see the full file, but really what's happening is that you've declared some i variable in one block, then within that block, declared another i variable.
Related
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.
Given a series of modules whose names follow a pattern:
module "mod-a" {
// ...
}
module "mod-b" {
// ...
}
module "mod-b" {
// ...
}
and assuming each module defines an output called my_output, can I refer to a particular module on the basis of a dynamically resolved name?
e.g.
...
// some_module = "mod-a"
some_value = module[some_module].my_output
...
The syntax above gives the error:
The "module" object cannot be accessed directly. Instead, access one of its
attributes.
Is there another way to access a module whose name is only known at runtime?
To achieve this in Terraform today (Terraform 0.12.13), you'll need to construct a suitable map explicitly as a local value, and then index that map:
locals {
modules = {
mod_a = module.mod_a
mod_b = module.mod_b
mod_c = module.mod_c
}
}
Elsewhere in the configuration you can then use an expression like local.modules[local.dynamic_module_key], selecting the desired object from the map.
Terraform requires static references to objects like this so that it can properly construct a dependency graph. In this case, Terraform infers that local.modules depends on all of the outputs from all three of these modules, and thus anything that refers to local.modules must wait until all of the outputs from all of these modules are ready to ensure that the final index operation has a complete value to work with.
I have inherited a project with two files:
a.tf
dir_name/b.tf
and each contains:
variable "region" {
default = "us-east-1"
}
Is there any reason why I can't delete the variable definition from dir_name/b.tf as it seems to be already defined?
UPDATE
a.tf contains a module definition that goes like this:
module "dir_name" {
source = "./dir_name"
}
No, you can't remove one or the other. Terraform works on the module level, where each module has an explicit set of input variables and output attributes; variables can't be passed implicitly from one script to a module.
If you're passing variables from one place to another, it does currently result in a lot of repetition, eg:
module "dir_name" {
source = "./dir_name"
region = "${var.region}"
}
I have created a temp.groovy script:
def loggedMessages = [];
def log(message) {
loggedMessages << message;
}
log "Blah"
print loggedMessages.join('\n')
When I run it I get an exception relating to the log method:
groovy.lang.MissingPropertyException: No such property: loggedMessages
Why can't the log method see the loggedMessages variable? I obviously have a fundamental misunderstanding of what's going on. I've tried to read the docs but I can't seem to find the relevant section.
From the comments, as an answer:
The Script class documentation states (in the last section 3.4 Variables) that:
if the variable is declared as in the first example [with a type definition], it is a local variable. It will be declared in the run method that the compiler will generate and will not be visible outside of the script main body. In particular, such a variable will not be visible in other methods of the script
And further: if the variable is undeclared, it goes into the script binding. The binding is visible from the methods [...]
Therefore removing the def from loggedMessages fixes the error:
loggedMessages = [];
def log(message) {
loggedMessages << message;
}
log "Blah"
print loggedMessages.join('\n')
(Alternatively it can be annotated using #groovy.transform.Field as #aetheria pointed out in the comments)
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);