【问题标题】:Node JS: TypeError: Cannot read property 'forEach' of undefined节点 JS:TypeError:无法读取未定义的属性“forEach”
【发布时间】:2020-09-25 14:15:34
【问题描述】:

我得到 TypeError: Cannot read property 'forEach' of undefined。我不知道下面的代码有什么问题:

app.post("/entershop", (request, response)=> {         

let username = request.body.uname;
let psw = request.body.psw; 

let rawdata = fs.readFileSync('mapping.json');
let rootdata = JSON.parse(rawdata);
let isvalid = false;
let shops = rootdata["shops"];

shops.forEach(function(shop) {
var password = shop[username][0];
if(password === psw) 
    {
       console.log('successful login');

    }
else 
    {
       console.log('login unsuccessful');
    }
  });
 });

网页中显示如下错误:

TypeError: Cannot read property 'forEach' of undefined
at app.post (/home/ubuntu/TheRandomShops/index_test.js:33:15)
at Layer.handle [as handle_request] (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/TheRandomShops/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/index.js:335:12)
at next (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/index.js:275:10)
at serveStatic (/home/ubuntu/TheRandomShops/node_modules/serve-static/index.js:75:16)
at Layer.handle [as handle_request] (/home/ubuntu/TheRandomShops/node_modules/express/lib/router/layer.js:95:5

mapping.json 如下所示:

{
"owners":[
            {"name":"Bala", "id":"1", "psw":"bala"},
            {"name":"Rohit", "id":"2", "psw":"Rohit"}
           ],
"employees":[
            {"name":"Krithi", "owners":"Bala", "psw":"Krithi"},
            {"name":"Kumar", "owners":"Rohit", "psw":"Kumar"}
           ]
 }

非常感谢您的帮助。

【问题讨论】:

  • 您解析的数据没有shops 属性。有ownersemployees 属性,但没有shops。所以rootdata["shops"]undefined
  • 让 rawdata = fs.readFileSync('mapping.json');让 rootdata = JSON.parse(rawdata);这两个需要一些时间,所以在这两个完成之前执行其余代码
  • @SfaheequeTP readFileSync 是...同步的。

标签: javascript jquery node.js arrays json


【解决方案1】:

希望这能解决您的问题

app.post("/entershop", (request, response)=> {         

let username = request.body.uname;
let psw = request.body.psw; 

let rawdata = fs.readFileSync('mapping.json');
let rootdata = JSON.parse(rawdata);
let isvalid = false;
let shops = rootdata["shops"]||[];

shops.forEach(function(shop) {
var password = shop[username][0];
if(password === psw) 
    {
       console.log('successful login');

    }
else 
    {
       console.log('login unsuccessful');
    }
  });
 });

【讨论】:

    【解决方案2】:

    rootdata 为空,因为您的数据中没有任何名为 shop 的内容

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      相关资源
      最近更新 更多