Hello wonderful glitch community,
My code is not currently saving to the sqlite3 database like it should
async function setItem(id, obj) {
await endb.set(id, JSON.stringify(obj));
}
async function getItem(id) {
//console.log("Getting");
//console.log(await endb.get(id));
var item = await endb.get(id);
console.log(item);
return JSON.parse(item);
}
function startNew(client, msg, saveID, playerID) {
if (!endb.has(playerID)) {
setItem(playerID, template).then(function() {
getItem(playerID).then(function(temp) {
temp["saves"][saveID] = {
curPos: 1
};
setItem(playerID, temp).then(function() {
displayMSG(
client,
msg,
story[endb[playerID]]["saves"][saveID]["curPos"]
);
});
});
});
} else {
// endb[playerID]
getItem(playerID).then(function(temp) {
temp["saves"][saveID] = {
curPos: 1
};
setItem(playerID, temp).then(function() {
displayMSG(
client,
msg,
story[endb[playerID]]["saves"][saveID]["curPos"]
);
});
});
}
}
I am getting the error
undefined
(node:12137) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at getItem (/app/stories.js:28:15)
at <anonymous>
(node:12137) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12137) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Update new code(fixed mistakes):
function startNew(client, msg, saveID, playerID) {
if (!endb.has(playerID)) {
setItem(playerID, template).then(function() {
getItem(playerID).then(function(temp) {
temp["saves"][saveID] = {
curPos: 1
};
setItem(playerID, temp).then(async function() {
let data = await getItem(playerID);
displayMSG(client, msg, story[data["saves"][saveID]["curPos"]]);
});
});
});
} else {
// endb[playerID]
getItem(playerID).then(function(temp) {
temp["saves"][saveID] = {
curPos: 1
};
setItem(playerID, temp).then(async function() {
let data = await getItem(playerID);
displayMSG(client, msg, story[data["saves"][saveID]["curPos"]]);
});
});
}
}
After setting an value for a key is it not available instantly? I know I have some other mistakes in here but please ignore them.
Update: Opened the terminal and did endb.all() and noticed nothing actually made it to the database.
FIXED: At if (!endb.has(playerID)) {
, endb.has
returns a promise I forgot.