简体   繁体   English

显示现有属性时控制台显示未定义 object

[英]Console showing undefined when displaying the properties of a existing object

var errors = validationResult(req);

console.log("errors: errors");

if (!errors.isEmpty()) {
  res.render('signIn-signUp', {
    'error1': errors.msg + " " + errors.value + " Please signUp again!!"
  });
} else {
  var id = 0;
  pool.query("Select Max(C_Id) as C_Id from customer", function(error, rows) {
        if (error) {
          console.log("Database error: ", error);
          res.render('signIn-signUp', {
            'error1': "database error: " + error + "please sign up again!"
          });

The above code is part of a nodeJs codebase.上面的代码是 nodeJs 代码库的一部分。 In the above code snippet, when I log errors object on console, it shows all the properties as:在上面的代码片段中,当我在控制台上记录错误 object 时,它将所有属性显示为:

errors: [{
  value: 'a',
  msg: 'Must be at least 5 char long',
  param: 'Password',
  location: 'body'
}]

but, when I display the individual properties of the object on the console, it is showing it as undefined.但是,当我在控制台上显示 object 的各个属性时,它显示为未定义。 That is, console.log(errors.msg) is returning undefined.也就是说,console.log(errors.msg) 返回未定义。 What is the reason for that?这是什么原因?

How can I display the properties which exist in the object and are not undefined?如何显示 object 中存在且未定义的属性?

It seems that the errors object is an array (hence the [] ). errors object 似乎是一个数组(因此是[] )。 Try this:试试这个:

    console.log(errors[0].msg);

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

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