TypeError:无法读取属性' length'未定义的nodejs

时间:2018-03-22 11:48:56

标签: javascript node.js express

从nodemon正常运行,直到我进入浏览器注册并收到错误消息" TypeError:无法读取属性' length'未定义" 当我决定评论if(" req.body.password.length> 4)时,注册没有工作,抱怨密码没有匹配。 请帮助我对Node

非常新

https://gist.github.com/dngoyb/de020c2686a1104af748593d37e0e082

这是完整的错误

TypeError: Cannot read property 'length' of undefined
    at router.post (/home/zidiou/Documents/Vidjot/Vidjot/routes/users.js:26:28)
    at Layer.handle [as handle_request] (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/layer.js:95:5)
    at /home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:335:12)
    at next (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:174:3)
    at router (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:317:13)
    at /home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:335:12)
    at next (/home/zidiou/Documents/Vidjot/Vidjot/node_modules/express/lib/router/index.js:275:10)
    at /home/zidiou/Documents/Vidjot/Vidjot/app.js:59:4

1 个答案:

答案 0 :(得分:0)

正确设置HTML非常重要,因此服务器知道正文中可以访问哪些数据。

此代码在1年前适用于我,在JS文件中,有一些你不需要的sdk,只需忽略它们,并尝试将其设置为满足您的需求。

HTML:

  form(action="", method="post", enctype="multipart/form-data")
    label(class="label", for="name") User:
    input(class="input", name="name")
    label(class="label", for="passwort") Passwort:
    input(class="input", type="password", name="passwort")
    input#button(type="submit", value="OK")

JS

var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
var wait = require('wait.for');
var app = express();
var session = require('express-session');
var json2csv = require('json2csv');
var json2pdf = require('json2pdf');
var fs = require('fs');

    AWS.config.update({
      accessKeyId: 'XXX',
      secretAccessKey: 'XXX',
      region: 'eu-central-1'
    });

    var docClient = new AWS.DynamoDB.DocumentClient();

    app.use(session({
      secret: 'test',
      resave: false,
      saveUninitialized: false,
      cookie: { secure: false}
    }))


    app.set('view engine', 'pug');
    app.use(express.static('public'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(upload.array());



              app.post("/", function(req, res) {
                  ssn = req.session;

              // DATABASE QUERY
              var user = {
                TableName: "user-table",
                KeyConditionExpression: "#usr = :user",
                ExpressionAttributeNames: {
                  "#usr": "User",
                },
                ExpressionAttributeValues: {
                  ":user": req.body.name
                },
              };

              docClient.query(user, function(err, data) {
                if (data.Count == 0 || req.body.passwort != data.Items[0].Passwort) {
                // do stuff here