简体   繁体   English

Node.js 405(方法不允许)和列不能是 null MySQL

[英]Node.js 405 (Method Not Allowed) and Column cannot be null MySQL

I'm trying to post data to my database.我正在尝试将数据发布到我的数据库。 I have two problems.我有两个问题。 Problem 1: I get a response that method is not allowed.问题 1:我得到的响应是不允许使用该方法。 I've put the images below我把图片放在下面

[enter image description here][1] [在此处输入图片描述][1]

The second problems I have is that my data keeps returning null when trying to insert the user's comments into MySQL database:我遇到的第二个问题是,当尝试将用户的评论插入 MySQL 数据库时,我的数据不断返回 null:

Here's the error: [enter image description here][2]这是错误:[在此处输入图像描述][2]

Here's my code:这是我的代码:

superhero.html:超级英雄.html:

<body>

    <div class="container">
        <h1> Welcome To Chatroom</h1>

        <div class="card mb-2" style="width: 38rem;" id="output">
    
        </div>

        <form class="inputs" id="form" name="form">
            <textarea class="comments" id="comments" placeholder="what's on your mind?" name="comments" ></textarea>
            <button type="submit" class="btn btn-primary" id="submitbtn">send</button>
        </form>

    </div>
    <script type="text/javascript" src="/javascript/superhero.js"></script>
    <script src="/reload/reload.js"></script>

</body>

superhero.js超级英雄.js

const output = document.getElementById('output');
const username = document.querySelector('#username');
const date = document.querySelector('#date');
const submitbtn = document.querySelector('#submitbtn');
const commentOutput = document.querySelector('#message');
const form = document.querySelector('#form');
const comments = document.querySelector('#comments')

form.addEventListener('submit', function(e) {
    e.preventDefault(e);


    let formMessage = new FormData(form);

    formMessage.append('api-key', 'myApiKey');


    fetch('http://localhost:5502/superhero', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify(formMessage)

    }).then(function(response) {
        console.log(response)
        return response.text();
    }).then(function(text) {
        console.log(text);
    }).catch(function(error) {
        console.log(error);
    });
})

index.js index.js


router.post("/superhero", function(req, res) {

    const user = req.user;
    const comments = req.body.comments;

    sqlDatabase.query("INSERT INTO comments (user_id, comments) VALUES (?, ?)", [user, comments],
        function(error, results, fields) {
            if (error) throw error;
            console.log(results);
            console.log(error)
        });
})


router.get("/superhero", authenticationMiddleware(), function(req, res, err) {

    sqlDatabase.query("SELECT users.username, comments.comments, comments.date FROM users INNER JOIN comments ON users.user_id=comments.user_id",
        function(error, results, fields) {
            if (error) throw error;
            res.render('superhero');
            console.log(results);
        })

})

I'm a noob by the way.顺便说一句,我是个菜鸟。 Been struggling on this.一直在为此苦苦挣扎。 Any help would be appreciated.任何帮助,将不胜感激。 [1]: https://i.stack.imgur.com/nG7Tq.png [2]: https://i.stack.imgur.com/JlgOp.png [1]: https://i.stack.imgur.com/nG7Tq.png [2]: https://i.stack.imgur.com/JlgOp.png

Finally fixed the problem.终于解决了问题。 I changed this line from:我将这一行从:

body: JSON.stringify(formMessage)

to

 body: JSON.stringify({ comments: comments.value })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM