I am using Node Monkey to debug my NodeJS app.
It happens frequently that when I click \"Store as Global Variable\" in my Chrome Console it says \"Failed to save to temp
I can see two reasons why Store As Global Variable wouldn't work:
1. Wrong Console Context Selected
This is arguably a Chrome bug, but you can only store an object as a global if the console is set to the same context as the code that logged the object.
In practice this means you might need to select the correct IFrame or web worker.
For example, on jsFiddle:
In the normal jsFiddle editor page context I'm getting an error. But it works if I change the context to the contents of the fiddle itself:
2. Garbage Collection
In order for Chrome to give you a reference to the object the object still has to be in memory. If that isn't the case it can only throw an error.
However, I'm pretty sure that being shown in the console forces V8 to keep a reference to the value.
You need to create the object in the Console itself, as the reference to the object needs to be maintained by Chrome. Just put the following into the console instead:
{why:'dont', you:'work?'}
If you check out this revision where the feature was added, it says:
Adding ability to access objects from printed ObjectPropertySections (console, scopes pane and etc.).
The problem, based on my understanding, is that console.log
is outputting a string representation of the object, and just using object formatters to display it nicely. The object doesn't exist anymore. When you create an object through the console itself, Chrome is storing the object itself in memory. If you have paused on a breakpoint and have locally scoped variables, these can also be stored globally because they are also in memory.
One thing you could do in your code, if you haven't got circular references is:
console.log(JSON.stringify({why:'dont', you:'work?'}));
> {"why":"dont","you":"work?"}
In the Console, copy the output and paste it into a JSON.parse
call:
JSON.parse('{"why":"dont","you":"work?"}');
> Object {why: "dont", you: "work?"}
The variable exists now in memory so you can store it.