Assign console.log value to a variable
You could override standard console.log()
function with your own, adding the behaviour you need:
console.oldLog = console.log;
console.log = function(value)
{
console.oldLog(value);
window.$log = value;
};
// Usage
console.log('hello');
$log // Has 'hello' in it
This way, you don't have to change your existing logging code. You could also extend it adding an array and storing the whole history of printed objects/values.
How can I add a variable to console.log?
Then use +
to combine strings:
console.log("story " + name + " story");
console.log() shows the changed value of a variable before the value actually changes
Pointy's answer has good information, but it's not the correct answer for this question.
The behavior described by the OP is part of a bug that was first reported in March 2010, patched for Webkit in August 2012, but as of this writing is not yet integrated into Google Chrome. The behavior hinges upon whether or not the console debug window is open or closed at the time the object literal is passed to console.log()
.
Excerpts from the original bug report (https://bugs.webkit.org/show_bug.cgi?id=35801):
Description From mitch kramer 2010-03-05 11:37:45 PST
1) create an object literal with one or more properties
2) console.log that object but leave it closed (don't expand it in the console)
3) change one of the properties to a new value
now open that console.log and you'll see it has the new value for some reason, even though it's value was different at the time it was generated.
I should point out that if you open it, it will retain the correct value if that wasn't clear.
Response from a Chromium developer:
Comment #2 From Pavel Feldman 2010-03-09 06:33:36 PST
I don't think we are ever going to fix this one. We can't clone object upon dumping it into the console and we also can't listen to the object properties' changes in order to make it always actual.
We should make sure existing behavior is expected though.
Much complaining ensued and eventually it led to a bug fix.
Changelog notes from the patch implemented in August 2012 (http://trac.webkit.org/changeset/125174):
As of today, dumping an object (array) into console will result in objects' properties being
read upon console object expansion (i.e. lazily). This means that dumping the same object while
mutating it will be hard to debug using the console.This change starts generating abbreviated previews for objects / arrays at the moment of their
logging and passes this information along into the front-end. This only happens when the front-end
is already opened, it only works for console.log(), not live console interaction.
console.log() displays the correct value, but when assigned it to a variable, it is undefined - Angular, firestore
It appears that you are not returning the Promise
from your function - there is no return statement in the getToken
function at all, so the function itself just returns undefined
. The internal return statement you have will resolve the promise, which is good, but you have to handle that resolution.
If you return the promise like so:
getToken(user_id) {
return this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
if (doc.exists) {
console.log(doc.data().user_token); //displays the correct user_token value
return doc.data().user_token;
}
});
}
You should be able to access the user_token
asynchronously when the promise resolves by doing the following:
getToken(user_id).then(user_token => {
handle_user_token_here(user_token);
});
Note: The function as modified will return a promise. Therefore, you cannot simply the following:
let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
handle_user_token_here(user_token); // will not work.
You can do this though:
let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
user_token.then(user_token => handle_user_token_here(user_token)); // will work
JavaScript console prints assigned value of variable before it has been assigned?
Try debugging your issue in the Chrome script debugger. Put a breakpoint on the line:
for (var i = 0; i < initial_array.length; i++) {
and you will see the behaviour you desire.
The problem you are having is you are making the incorrect assumption that the Chrome debugger 'prints' the value immediately when in fact it does the console.log
asynchronously. Since arrays are passed around by reference in the backend when it actually goes to print the value it is now the one you are seeing.
In Chrome console, how do you set data you've outputted into console into a local variable?
Here is how, in case helpful to anybody in the future.
STEP 1: Identify the data you want to put into a variable
STEP 2: Right click on the data and click "Store object as global variable"
STEP 3: This new variable will be outputted in the next line. Usually temp1
STEP 4: Now use the dot notation to navigate through the data.
You can use this variable as you wish; I use dot notation to navigate through the data. This helps in case, I need to navigate through data or array in code and I want to confirm that I am doing it correctly.
Related Topics
How to Show Modal Window in Controller's Action
Template Language That Works on Both Server and Client
How to Disable Browser Developer Tools
Rails 5 - Turbolinks 5,Some Js Not Loaded on Page Render
How to Pause a Youtube Player When Hiding the Iframe
Most Efficient Method of Detecting/Monitoring Dom Changes
How to Use JavaScript in Ruby on Rails
How to Split a Long Regular Expression into Multiple Lines in JavaScript
Wkwebview - Complex Communication Between JavaScript & Native Code
How to Use Source: Function()... and Ajax in Jquery UI Autocomplete
Rails Specify Load Order of JavaScript Files
How to Get an Ajax Get-Request to Wait for the Page to Be Rendered Before Returning a Response
Rails 4 Client Side Validation
How to Count the Number of Occurrences of Each Item in an Array