How to store objects within a replit database?

Viewed 430

I'm having some trouble with the repl.it database. I'm trying to store a bunch of data as well as an object within it using the following code:

db[message.author.id] = {
    "creation_date": datetime.datetime.now().strftime("%Y-%m-%d"),
    "tutorial_completed": False,
    "money": 0,
    "properties": [shack]  # < the object
}

However, whenever i try to run it, i get the following error:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 116, in on_message
    db[message.author.id] = {
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 491, in __setitem__
    self.set(key, value)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 500, in set
    self.set_raw(key, _dumps(value))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 56, in dumps
    return json.dumps(val, separators=(",", ":"), cls=DBJSONEncoder)
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

It works completely fine when I remove the object so it seems that that is causing the problem. Any idea of why this is happening or what could be a work around? Thanks.

1 Answers

You could try encoding it as a JSON object using the json module, by first converting it to a string, then storing that. I did a similar thing once using environment variables, but can't remember the specifics.

Related