I have two html documents, and I want them to share a variable and its value.
In lieu of 'truly global' variables in JavaScript, I've tried employing Web API Storage:
var number = parseInt(localStorage.setItem('num',0));
Storage is always a string, so I try parsing into an integer seemingly incorrectly (rather than showing up as 0 in my program, it shows up as 'NaN'). I can only assume that my syntax's wrong?
I want this variable to be increaseable (+1 every time the user clicks on something) but have not yet figured this out. I then want to retrieve its value in the second html document. But due to the initial misstep in attempting to parse the storage value I cannot yet attempt these methods.
It doesn't work like that because localStorage.setItem returns undefined. The way to do it would be:
// define variable
var num = 0;
// set to storage
localStorage.setItem('num', num);
// get from storage
num = parseInt(localStorage.getItem('num'));
// write-back incremented
localStorage.setItem('num', num + 1);
localStorage.setItem stores the value but doesn't return anything - or in other words returns undefined. Trying to parse undefined as a number returns NaN.
Note that localStorage does not work across files when viewing files in your browser from file://, you'll need to run a minimal server to test your code.
Related
In my post processor I have the following code:
if (i as Integer < protocolsArray.length){
protocolsArray[i as Integer] = "${requestProtocolId}" ;
}
and it worked, I put the "requestProtocolId" that I got via JsonExtractor in the position of "i", position of i == 0
but the number of virtual users defined in this http request is greater than one.
so it sends the request again and saves the new "requestProtocolId" again in position zero, overwriting the other protocol,I understand that with the new request it starts all over again, taking the initial values assigned to the variables again, but I've already tried incrementing the i (i ++) and returning the new array with the zero position filled in:
vars.putObject("protocolsArray", protocolsArray);
but it always returns the value set before the htpp request, is there a way to change that?
If I changed and put an iteration controller and it was a group of users but in iteration controller "5", it would be like the same user would send it five times, right?
I wanted to simulate different users, but always keep the "requestProtocolId" value saved in the array positions because I'm going to use it in another request.
In its current state your question doesn't make a lot of sense.
JMeter Variables are local to the thread (virtual user)
If the user starts new iteration - the variable will be still there, if this is not something you want/expect - use vars.remove() function to ensure that the variable is always "fresh"
Don't inline JMeter Functions or Variables in scripts, in case of Groovy (the recommended scripting option) only first occurrence will be cached and used during other iterations, see JSR223 Sampler documentation for more information
Disclaimer; my background is in C#, so I'm not familiar with JavaScript more than at a cursory level; however, I was reviewing some code with one of our developers today and he was receiving some strange behavior of return results "merging" with pre-existing return results. I was very surprised to see every single JavaScript example of calling a Stored Procedure uses a Global Variable on the MySQL side to store the out parameter! This blows my mind and IMO is a big "no no" relative to writing code. Take the below example:
exports.updateRasSql = 'CALL update_ras_data(?, ?, #out_result, #out_result_value);
What it comes down to is we do NOT want to assign the OUT parameter variables to a MySQL global variable. We want the OUT values to be assigned to internal JavaScript variables, but any time we try to do this (using var or let as a definer), MySQL returns:
Error: ER_SP_NOT_VAR_ARG: OUT or INOUT argument 3 for routine empowercrm_main.update_ras_data is not a variable or NEW pseudo-variable in BEFORE trigger
So here's the question: How do we call a MySQL Stored Procedure from Node/JavaScript and have it return the OUT variables to internal JavaScript variables and not MySQL global variables?
You should return a result set (make query in a procedure) instead of using OUT-variables. The only way of getting the value of a OUT-variable is to select it after you have executed the procedure. It's easier just to handle the result set.
I am using Cloud Firestore and am having some problems accessing a value in a simple database.
Here is how the database is structured
This is the code I am using to access the "basementER-status" field in the database.
//current status value is pulled from database
function getRawStatus ()
{
return db.collection("rooms").doc("roomsDoc").get().then(function(doc) {
console.log(doc.data());
console.log(doc.data().basementER-status);
return doc.data().basementER-status;
});
}
For the first console.log, this is printed to the console:
{1ER-status: 0, 2ER-status: 0, basementER-status: 0}
1ER-status: 0
2ER-status: 0
basementER-status: 0
__proto__: Object
This is the correct doc that needs to be brought from the database, so I know that part of my code is right.
However, the second console.log prints NaN to the console.
What is happening here? I don't understand. I've accessed fields like this before in cloud firestore and it has always worked.
For your second console.log statement, you are trying to access a specific parameter in an object, so you should use this:
console.log(doc.data()["basementER-status"]);
For more info, go here: firestore adding data link
The problem is because of the way JavaScript parses your statement. This line:
console.log(doc.data().basementER-status);
Is actually performing a mathematical subtraction between doc.data().basementER and the value of the variable status. That's not what you want.
If you want the value of a field with JavaScript operators or other special characters in it, you will have to use a different syntax:
const data = doc.data();
console.log(data['basementER-status']);
The square brackets let you provide an arbitrary string to look up the name property in the object.
I want to set the local storage with unique key with "DONE" value
for example :
PdfSeen as key DONE as value
VideoSeen as key DONE as value
both
localStorage.setItem("PdfSeen" , "DONE");
localStorage.setItem("VideoSeen" , "DONE");
set a new entry in the local storage with
key DONE and value undefined
my expected result is
PdfSeen as key DONE as value
VideoSeen as key DONE as value
please check this below image to understand the issue well
the below code leads to the above entry in the localstorage
I think I found your issue. You have to pull down your bar on local storage. See the following images:
Now use the cursor to drag the line some pixels down and you will see the real keys:
undefined in the console only means, that there was no return value.
localStorage.setItem("PdfSeen", "DONE"); // will log `undefined`
console.log(localStorage.getItem("PdfSeen")); // will log the value.
also see Using the Web Storage API.
So I'm trying to save an object via the chrome.storage API. The relevant code is:
var images = null;
var storage = chrome.storage.sync;
var key = "key";
images = getImages(source);
alert(images.length); // returns 4
storage.set({key: images});
storage.get("key", function(result) {
alert(result.length); // returns undefined
});
I'm tested that immediately after the getImages() function, images is a wrapped set JQuery object with a length of 4. However, when I try to access images.length via the storage.get callback, the result is undefined.
Could someone help identify the error in how I am storing and/or retrieving this JQuery object?
Update 1:
Thank you all for your help. As clarification for the use case, I am using chrome.storage instead of localStorage because I plan to pass extension info to another script.
Fortunately, TranQ/Xan's solution has enabled me to access the array via the storage.get call.
I'm still experiencing issues working with the wrapped set JQuery object stored in the array but I'll post a separate question since the current solution encapsulates broader use cases.
TranQ's comment is on point.
Presumably, images is an array. You store that array under the "key" key.
When you execute the get() function, it returns an object populated with all key-value pairs you asked, even if you only ask for one key.
So, result is an object {key : [/* something */]}. Objects do not have a length property, and you get undefined
You need to use result.key (or result["key"]) to access your array.