how to use variable value outside change function
1, Declare selected_gender variable before on change function called.
2, take value of clickd element in selected_gender variable
3, print that variable
Hope below code helps you to understand better.
let selected_gender = "";
$('input[type=radio][name=pop_gender]').on('change', function() {
selected_gender = this.value; //this will get value of input type is radio and name is pop_gender which was last time clicked
console.log(selected_gender); //in side of function
});
console.log(selected_gender); //out side of function
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input type="radio" name="pop_gender" id="gender_female" value="Female">
<input type="radio" name="pop_gender" id="gender_male" value="Male">
</body>
</html>
Getting a let value outside a function
For your new database schema, try this instead:
private func loadData() {
dbRef!.observe(.childAdded) {
(placeSnapshot) in
print("Adding place \(placeSnapshot.key)...")
// Load each label on the "placeLabel" list.
let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")
for (key, label) in labels.value as! [String: String] {
self.updatePlace(key, label: label)
}
// Load each rating in the "rating" list.
let ratings = placeSnapshot.childSnapshot(forPath: "rating")
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
}
}
The updatePlace
function simply groups the label and rating properties, of a given place, that were separately loaded by loadData
.
Places removal. Of course, the above code won't handle place removal. To do so, you will need to listen for the childRemoved
event as well.
Get and return value outside function - Nodejs
wrap with Promise
let user_id = req.query.user_id;
let chat_name = req.query.chat_name;
var chimesdkmessaging = new AWS.ChimeSDKMessaging({region: region});
let channel = null;
try {
let dateNow = new Date();
const params = {
Name: chat_name,
AppInstanceArn: config.aws_chime_app_istance,
ClientRequestToken: dateNow.getHours().toString() + dateNow.getMinutes().toString(),
ChimeBearer: await AppInstanceUserArn(user_id),
AppInstanceUserArn of the user making the API call.
Mode: 'RESTRICTED',
Privacy: 'PRIVATE'
};
channel = await new Promise((resolve,reject)=>{
chimesdkmessaging.createChannel(params, function (err, data) {
if (err) {
console.log(err, err.stack);
reject(err)
} // an error occurred
else {
console.log(data); // successful response
console.log('Assegno il canale: ' + data.ChannelArn);
resolve(data.ChannelArn)// <----- VALUE I WANT TO RETURN OUTSIDE
}
});
})
} catch (e) {
return response({error: e}, 500);
}
return response({data: channel},200); // <---- but this channel is null
}
If a variable is created within the scope of a function that returns a function that uses that variable. Is it possible to acces that variable outside
So, the comment you wrote about printNum() function being able to access the num
variable somehow is due to JavaScript closures. Closures are an important JavaScript topic which I would recommend looking at some online resources or this stackoverflow post JavaScript closures to learn about. Essentially when you return the printNum function from createPrintNumFunction, the printNum function will remember the num variable that was declared in createPrintNumFunction. But to answer your question of whether you can access the variable outside of the scope it was declared in, then the answer is no. An outer scope cannot access variables from an inner scope. Inner scopes can access variables from outer scopes. So, unless you are keeping track of the num
variable from outside of the scope that it was declared in, you won't be able to access the value num
holds outside of the scope it was declared in.
So, here you can change the value of num
inside of the printNum function itself like this:
function creatPrintNumFunction() {
var num = 12;
return function printNum() {
num = 13;
console.log(num);
};
}
var printer = creatPrintNumFunction();
printer.num = 13; //this part doesn't work but is there a way to access this Num variable Outside the creatPrintNumFunction, The printer function must be finding the num variable from somewhere.
printer();
how to access a local variable value outside the scope of its function in nodejs
Let's first explain why you don't get the expected result, it doesnt have to do with scope actually:
import csv from 'csv-parser'
import fs from 'fs';
let budget_details
const budgetProcessing = (budget_file_path) => {
try{
fs.createReadStream(budget_file_path)
.pipe(csv())
.on('data', (row) => {
budget_details = row
})
.on('end', () => {
console.log('CSV file successfully processed');
});
}
catch(error){
console.log(error)
}
}
budgetProcessing('budget.csv')
console.log(budget_details)
fs.createReadStream
is not itslef exactly asynchronous but then we pipe the returned stream to csv-parser
which does event based parsing, so even if you call budgetProcessing before the console.log(budget_details)
the stream reading has most likely not runned yet and budget_details
is still undefined.
To fix this, you could move this console.log(budget_details)
where it is set like so:
let budget_details
const budgetProcessing = (budget_file_path) => {
try{
fs.createReadStream(budget_file_path)
.pipe(csv())
.on('data', (row) => {
budget_details = row
console.log(budget_details)
})
.on('end', () => {
console.log('CSV file successfully processed');
});
}
catch(error){
console.log(error)
}
}
budgetProcessing('budget.csv')
but then the variable itself wouldnt serve any real purpose so instead you could do this:
const budgetProcessing = (budget_file_path, callback) => {
try{
fs.createReadStream(budget_file_path)
.pipe(csv())
.on('data', (row) => {
callback(row)
})
.on('end', () => {
console.log('CSV file successfully processed');
});
}
catch(error){
console.log(error)
}
}
budgetProcessing('budget.csv', (budget_details) => {
console.log(budget_details) // or anything with budget_details
})
Lastly, I want to make clear that the callback will be called for each row of the csv as specified in csv-parser's documentation
Related Topics
How to Load My Own Reality Composer Scene into Realitykit
Swift Can't Infer Generic Type When Generic Type Is Being Passed Through a Parameter
If the Swift 'Guard' Statement Must Exit Scope, What Is the Definition of Scope
Swift Utf8 Encoding and Non Utf8 Character
Define a Swift Protocol Which Requires a Specific Type of Sequence
Add Environmentobject in Swiftui 2.0
How to Bring Nswindow to Front and to the Current Space
Fill Circle with Wave Animation in Swiftui
Swift: Floating Plus Button Over Tableview Using the Storyboard
How to Request a Desktop Version of a Webpage Using Uiwebview in Swift 3.0
Redeclaring Members in an Extension Hides the Original Member *Sometimes*. Why
Get Signed Integer from Swift String of Binary
Compare Three Values for Equality
Swift 5 Table View Cell with Uiimage Appears Very Tall and Image Extremely Zoomed