database-node
database-node copied to clipboard
fix: fetch is not defined
was just trying to create simple todo to teach one friend how simple replit is, but it didn't go well
repl: https://replit.com/@KaranMJ/WindyLastChords#index.js
error:
list of todo: {
ok: false,
error: { message: 'fetch is not defined' },
errorExtras: undefined
}
{
ok: false,
error: { message: 'fetch is not defined' },
errorExtras: undefined
}
recived this todo, asdas
{
ok: false,
error: { message: 'fetch is not defined' },
errorExtras: undefined
}
existing todos {
ok: false,
error: { message: 'fetch is not defined' },
errorExtras: undefined
}
set the following todos: {
ok: false,
error: { message: 'fetch is not defined' },
errorExtras: undefined
}
code:
const express = require('express');
const Database = require("@replit/database");
const bodyParser = require('body-parser');
const app = express();
const db = new Database();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', function(req, res) {
if (req.header('X-Replit-User-Id')) {
res.redirect(`/home/${req.header('X-Replit-User-Name')}`);
} else {
res.sendFile(__dirname + '/public/login.html');
}
});
app.get('/home/:user', async function(req, res) {
const user = req.params.user;
try {
console.log("list of todo:",await db.list())
let todos = await db.get(`todos_${user}`);
console.log(todos);
todos = Array.isArray(todos) ? todos : [];
res.send(`
<h1>Welcome, ${user}!</h1>
<h2>Your Todo List:</h2>
<ul>
${todos.map(todo => `<li>${todo} <form style="display:inline" action="/delete/${user}/${encodeURIComponent(todo)}" method="POST"><button type="submit">Delete</button></form></li>`).join('')}
</ul>
<form action="/add/${user}" method="POST">
<input type="text" name="todo" required>
<button type="submit">Add Todo</button>
</form>
`);
} catch (error) {
console.error('Error fetching todos:', error);
res.status(500).send('An error occurred while fetching your todos.');
}
});
app.post('/add/:user', async function(req, res) {
const user = req.params.user;
const todo = req.body.todo;
try {
console.log("recived this todo, ", todo);
console.log(await db.getAll())
let todos = await db.get(`todos_${user}`);
console.log("existing todos", todos);
todos = Array.isArray(todos) ? todos : [];
todos.push(todo);
const result = await db.set(`todos_${user}`, todos);
console.log("set the following todos:", result);
// res.redirect(`/home/${user}`);
res.status(200)
} catch (error) {
console.error('Error adding todo:', error);
res.status(500).send('An error occurred while adding your todo.');
}
});
app.listen(8080, function() {
console.log('Todo app server up!');
});
yo @bradymadden97
and also couldn't understand why issues are off on this repo?
@KMJ-007 hey, this repo is heavily unmaintained and only updated when replit changes something I've had times where I've pushed for years to fix fundamental bugs, they finally fixed them recently, but I'm not sure when they'll get back at it. In the mean time you can try my fork which I made exactly for the reasons above (it should easy to use as this and faster)
@KMJ-007 in this case the issue is not related to the package though, I see you are using old NodeJS (possibly old template), and that version of NodeJS does not have top level fetch API, I suggest doing this to fix the issue:
In .replit at the top add
modules = ["nodejs-20"]
In replit.nix remove pkgs.nodejs-16_x
wait what
i didn't realised, i was giving the ai code generation demo to friend and that's how this mess came into existence
wait what
i didn't realised, i was giving the ai code generation demo to friend and that's how this mess came into existence
Hey, my first comment doesn't fix the issue here, it's just me saying I have a better package for the replit db, the second comment is where I realized what the issue is and gave you two simple steps to fix it.
@KMJ-007 you can close this now. It's not an issue with the package.