Ask coding questions

← Back to all posts
Getting unwanted result from replit db.
OldWizard209 (1624)

So I created a database in Node and set the key to a username. But when I try to print it out, I get this:

const Database = require("@replit/database")
const db = new Database()

var username = process.env.REPL_OWNER 
var stats = [10000/*Money*/, 0/*Bank*/, 1000/*Deposited*/, ""/*Occupation*/, []/*Inventory*/]

db.set(username, stats).then(() => {})

console.log("Moeny: "+ db.get(username).then(value => {}))

I get:

Moeny: [object Promise]

Why am I getting this and how can I return the value assigned to the key?

Answered by ruiwenge2 (1135) [earned 5 cycles]
View Answer
Comments
hotnewtop
ruiwenge2 (1135)

because it’s a promise you must do this:

db.get(username).then(value => {
  console.log("Moeny: " + value);
}));
OldWizard209 (1624)

Yeah, but when I change the code to this:

const Database = require("@replit/database")
const db = new Database()

var stats = {
  "Money": 10,
  "in": [1, 2]
}

db.set("key", stats).then(() => {});

db.get("key").then(value => {
  console.log("Moeny: " + value);
});

I get

Moeny: [object Object]

Any idea why?

@ruiwenge2

ruiwenge2 (1135)

@OldWizard209 thats because the value is a dictionary.
try this:

console.log("Moeny: " + JSON.stringify(value));