Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

fix: fetch is not defined#58

Closed
KMJ-007 wants to merge 1 commit intoreplit:masterfrom
KMJ-007:patch-1
Closed

fix: fetch is not defined#58
KMJ-007 wants to merge 1 commit intoreplit:masterfrom
KMJ-007:patch-1

Conversation

@KMJ-007
Copy link

@KMJ-007 KMJ-007 commented Aug 1, 2024

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!');
});

@KMJ-007
Copy link
Author

KMJ-007 commented Aug 1, 2024

yo @bradymadden97

@KMJ-007
Copy link
Author

KMJ-007 commented Aug 1, 2024

and also couldn't understand why issues are off on this repo?

@7heMech
Copy link

7heMech commented Aug 1, 2024

@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)

@7heMech
Copy link

7heMech commented Aug 1, 2024

@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

@KMJ-007
Copy link
Author

KMJ-007 commented Aug 1, 2024

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

@7heMech
Copy link

7heMech commented Aug 1, 2024

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.

@7heMech
Copy link

7heMech commented Aug 3, 2024

@KMJ-007 you can close this now. It's not an issue with the package.

@KMJ-007 KMJ-007 closed this Aug 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants