Skip to content
← Back to Community
Getting unwanted result from replit db.
Profile icon
OldWizard209

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 [earned 5 cycles]
View Answer
Voters
Profile icon
OldWizard209
Comments
hotnewtop
Profile icon
ruiwenge2

because it’s a promise you must do this:

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

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

Profile icon
ruiwenge2

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

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