【问题标题】:html input returns undefined with expresshtml输入返回未定义的快递
【发布时间】:2018-12-07 04:19:48
【问题描述】:

所以我第一次学习如何用 Mysql 做 Node.js。我目前正在关注本教程(https://hackernoon.com/setting-up-node-js-with-a-database-part-1-3f2461bdd77f)并且我被困在我运行节点的点(在标题“设置 Knex”之前)以及当用户在输入中输入他们想要的用户名和密码时。在教程中它说它应该 console.log 回用户的用户名和密码,但我得到了 undefined。

Server running on http://localhost:7555
Add user undefined with password undefined

我尝试查找如何解决它,但我似乎无法完成我的工作。我不太确定它是 express 还是 html 看起来已经过时了。这就是我现在所拥有的。

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Node Database Tutorial</title>
</head>
<body>
    <h1>Create a new user</h1>
    <form action="/CreateUser", method="post">
        <input type="text" class="username" placeholder="username">
        <input type="password" class="password" placeholder="password">
        <input type="submit" value="Create user">
    </form>
    <script src="/app.js"><script>
</body>
</html>

app.js

const CreateUser = document.querySelector('.CreateUser')
CreateUser.addEventListener('submit', (e) => {
    e.preventDefault()
    const username = CreateUser.querySelector('.username').value
    const password = CreateUser.querySelector('.password').value
    post('/createUser', { username, password })
})

function post(path, data){
    return window.fetch(path, {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
}

index.js

const express = require('express')
const bodyParser = require('body-parser')
const store = require('./store')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({extended: false})
app.post('/createUser', urlencodedParser, function(req, res){
        if (!req.body) return res.sendStatus(400)
        store.createUser({
          username: req.body.username,
          password: req.body.password
        })
        .then(() => res.sendStatus(200))
})

app.listen(7555, () => {
    console.log('Server running on http://localhost:7555')
})

请帮忙,我已经卡了几天了。

编辑:这是我的 console.log 所在的位置(store.js)

module.exports = {
  createUser({ usern-ame, password }) {
    console.log(`Add user ${username} with password ${password}`)
    return Promise.resolve()
  }
}

【问题讨论】:

    标签: html mysql node.js express


    【解决方案1】:

    在您的表单中,您的&lt;form&gt; 标记中没有class='CreateUser'。在那里添加类。

    另外,在您的app.post 中没有console.log

    store.js 语法错误,应该是:

    module.exports = {
      createUser: function({ username, password }) {
        console.log(`Add user ${username} with password ${password}`)
        return Promise.resolve()
      }
    }
    

    【讨论】:

    • 遗憾的是,这并没有解决我的问题;(console.log 位于 app.post 调用的 store.js 文件中。我将编辑我的原始帖子以获取它。
    • 感谢我尝试了您的更改,但仍未解决我未定义的问题;/
    • 现在正在打印什么?
    • 和之前服务器在localhost:7555上运行一样的东西添加用户未定义和密码未定义
    • console.log req.bodystore.createUser 之前并粘贴内容。还有console.log({username, password})post 调用之前的app.js 中。我们需要找出问题的根源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多