Node.js – die besten Beiträge

Wie kann ich den Fehler beheben?

const http = require('http');
const fs = require('fs');
const mysql = require("mysql2");

var db  = mysql.createConnection({
    host : "localhost",
    user : "root",
    password : "passwort",
    database : "test-datenbank"
});


db.connect(function (error){
    if (error) throw error;
    console.log("Connected!")
    var sql = "INSERT INTO benutzer (username) VALUES ('Beispiel')";
    db.query(sql, function (error,result){
        if(error) throw error;
        console.log("1 record inserted");
    });
    sql="SELECT * FROM benutzer"
    
    db.query(sql,function(error,result){
        if (error) throw error;
        console.log(result);

    });
});

const server = http.createServer((req,res) => {
    const url = req.url;
    const method = req.method;
    

    if ( url === "/"){
    res.setHeader("key","value");
    res.setHeader("Content-Type","text/html");
    res.write('</html><body>');
    res.write('<h1>Main</h1>');
    res.write('<h1><form action="/register" method="post"> <input type="text" name="Username"><br><button type="submit">Click</button></form></h1>');
    res.write("</body></html>");
    res.end()
    return;
    }

    if (url === "/register"){
         
        const reqBody = []
         req.on('data', (chunk)=>{
            console.log(chunk);
            reqBody.push(chunk);
     });
        
     req.on('end', () => {
        console.log(reqBody);
        const parsed = Buffer.concat(reqBody).toString();
        console.log(parsed);
        const user = parsed.split('=')[1];
        console.log(user)
        
        // funktioniert nicht 
        sql = 'INSERT INTO benutzer (username) VALUES ?, user;
        db.query(sql,function(error,result){
            if (error) throw error;
            console.log(result);
        });



});
        
        
        res.setHeader("key","value");
        res.setHeader("Content-Type","text/html");
        res.write('</html>');
        res.write('<body><h1>Registriert</h1></body>');
        res.write("</html>");
        res.end()
        return;

    }
    
}) 



server.listen(1337);

Der Fehler lautet wie folgt:

code: 'ER_PARSE_ERROR',

 errno: 1064,

 sqlState: '42000',

 sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1",

 sql: 'INSERT INTO benutzer (username) VALUES',

 fatal: true

SQL, Webentwicklung, node.js

Nodejs mariadb Column count doesn't match value count at row 1?

meine datenbank hat 4 spalten aber ich setze nur 3 ein weil die letzte eine auto_increment spalte ist,

ich nutze nodejs mit mariadb und habe folgende fehlermeldung beim daten einsetzen:

/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/misc/errors.js:64
  return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
         ^

SqlError: (conn=2420, no: 1136, SQLState: 21S01) Column count doesn't match value count at row 1
sql: INSERT INTO thread value (no, head, ts) - parameters:['1171187570193989762','rrrrrrrrrrrrrrrr',1699303276347]
    at module.exports.createError (/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/misc/errors.js:64:10)
    at PacketNodeEncoded.readError (/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/io/packet.js:582:19)
    at Query.readResponsePacket (/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/cmd/parser.js:58:28)
    at PacketInputStream.receivePacketBasic (/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/io/packet-input-stream.js:85:9)
    at PacketInputStream.onData (/home/j44/Downloads/themer/tut/node_modules/mariadb/lib/io/packet-input-stream.js:135:20)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:376:12)
    at readableAddChunk (node:internal/streams/readable:349:9)
    at Readable.push (node:internal/streams/readable:286:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  sqlMessage: "Column count doesn't match value count at row 1",
  sql: "INSERT INTO thread value (no, head, ts) - parameters:['1171187570193989762','rrrrrrrrrrrrrrrr',1699303276347]",
  fatal: false,
  errno: 1136,
  sqlState: '21S01',
  code: 'ER_WRONG_VALUE_COUNT_ON_ROW'
}


SQL, programmieren, JavaScript, Datenbank, MySQL, node.js, Discord, Discord Bot

Wieso funktioniert meine "Browser Extension" nicht auf dieser Seite?

Mir ist unter Linux aufgefallen das ich gar nicht den Auto-Scroll von Windows habe in zb. Google Chrome wenn man auf das Mauspad klickt, nutze ich gerne und dachte mir als Challenge ohne Chat Gpt dass ich mal schnell probieren könnte ein eigenes zu programmieren.

Ich weiß da gibt es fertige Software und Extension's, die laufen ohne Probleme und Ressourcen sparender wahrscheinlich.

Hab das erst in einer Html Datei (Extension in "") gemacht zum testen dann in anderen Websites den JS Code in eine Konsole eingefügt und es funktioniert eigentlich ganz gut.

Das Problem ist das ich nicht verstehe wieso er nicht hier funktioniert https://www.tennon.io/ , mir fällt beim inspizieren nichts auf und es scheint mit Next JS umgesetzt zu sein.

<!DOCTYPE html>
<html lang="de">


<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Auto Scroll Extension</title>
    <style>
        html {
            min-width: 100%;
        }
    </style>
</head>


<body>
    <script>
        /* Render Dummy Content, cursor change wont work with min-height */
        for (let i = 0; i < 1000; i++) {
            document.body.innerHTML += "Lorem Ipsum ";
        }
        const scrollSpeed_Up = -1;
        const scrollSpeed_Down = 1;
        const middlehalf = window.innerHeight / 2;
        let AutoScroll_Interval; /* Global acess */
        let isScrolling = false;
        let isScrollingUp = false; /* Default Scrolling Down, Prevent Errors */


        const ToggleScrolling = Enable => {
            if (Enable) {
                console.log("Auto Scroll Active");
                document.body.style.cursor = "all-scroll";
                AutoScroll_Interval = setInterval(AutoScroll, 1);
                isScrolling = true; /* Prevent Multiple Interval's */
            } else if (!Enable && isScrolling) {
                /* Stop Interval , Reset Cursor and Variables */
                console.log("Auto Scroll Stopped");
                document.body.style.cursor = "auto";
                clearInterval(AutoScroll_Interval);
                isScrolling = false;
            }
        }


        const AutoScroll = () => {
            /* Get Current Vertical Scroll Position : console.log(window.scrollY); */
            isScrollingUp ? window.scrollBy(0, scrollSpeed_Up) : window.scrollBy(0, scrollSpeed_Down);
        }


        window.addEventListener("mousemove", e => {
            e.clientY > middlehalf ? isScrollingUp = false : isScrollingUp = true;
        });


        /* Check if Scroll Button is Pressed */
        window.addEventListener("mousedown", e => {
            e.button === 1 && !isScrolling ? ToggleScrolling(true) : ToggleScrolling(false);
        });


        /* ############## IGNORE ##############
        Check if Client has reached Bottom
        const scrollableHeight = document.documentElement.scrollHeight - window.innerHeight;
        if (window.scrollY >= scrollableHeight) { 
            // Reached Bottom Code ... 
        } else {
            // Code ...
        }
        */
    </script>
</body>


</html>
Browser, Linux, HTML, IT, Webseite, JavaScript, Ubuntu, HTML5, Informatik, Programmiersprache, Webentwicklung, Frontend, React, node.js, React Native

Meistgelesene Beiträge zum Thema Node.js