Chrome DevTools error: “Failed to save to temp variable.”

偶尔善良 提交于 2019-12-05 08:29:28

问题


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 variable."

console.log({why:'dont', you:'work?'})

It happens also in this jsfiddle

1) Am I doing something wrong?

2) Why this happens?

Chrome: 50.0.2661.102 (64-bit) OSX El Capitan 10.11.4


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/37562683/chrome-devtools-error-failed-to-save-to-temp-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!