1

I'm using Blocktrail's API, in order to manage bitcoin wallets. The API returns a wallet object, which I guess has circular references contained within.

I want to store the decrypted wallet in the user's session, so that the user doesn't have to enter their password again, but whenever I try to do something like this (wallet being the wallet object returned by the API):

req.session.wallet = wallet;

I get:

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at MemoryStore.set

So, if an object has a circular structure, is there a way to store a reference to it in a session? Why does this happen?

Tarman
  • 13
  • 3

1 Answers1

1

You can't serialize circular structure into JSON string, so you can't store it in session.

You would have to strip all the circular references before serializing to JSON string.

timeiscoffee
  • 468
  • 3
  • 14
  • So I guess that the sessions' implementation tries to stringify the object, since sessions store strings, right? But wouldn't stripping out circular references interfere with the object's functionality? Isn't there a way to keep the object intact and make it persist the session? – Tarman Jun 30 '15 at 18:30
  • 1
    As per: http://stackoverflow.com/questions/7420597/javascript-how-to-save-an-object-with-circular-references, check out [JSON-js](https://github.com/douglascrockford/JSON-js). Supposedly it can store circular JSON using JSONPath. – timeiscoffee Jun 30 '15 at 18:58