te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - NodeJS TypeError: Cannot read property of undefined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS TypeError: Cannot read property of undefined - Stack Overflow

programmeradmin3浏览0评论

so I just started using NodeJs still have a lot to learn but am loving it so far I ran into this problem I found an answer, but it's not what I want it's kinda weird, let me show some code and explain

So I got 2 files

File 1 > app.js (Main)

File 2 > UserEntity.js (Module)

app.js:

var uEntity = require('./UserEntity.js');

var hm = new uEntity.person();
console.log(hm.info.strUsername);

UserEntity.js:

function User() {
    this.info = {
        'strUsername': 'Hashimoro'
    };
}

module.exports.person = User;

I am creating a new UserEntity I want to access the info directly as you can see from the code

But it gives this error:

console.log(hm.info.strUsername);
                   ^

TypeError: Cannot read property 'strUsername' of undefined
    at Object.<anonymous> (D:\NodeLearn\Testing\app.js:4:20)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

If someone can give a fix and explain would be great

Thank you, seriously appreciate it

so I just started using NodeJs still have a lot to learn but am loving it so far I ran into this problem I found an answer, but it's not what I want it's kinda weird, let me show some code and explain

So I got 2 files

File 1 > app.js (Main)

File 2 > UserEntity.js (Module)

app.js:

var uEntity = require('./UserEntity.js');

var hm = new uEntity.person();
console.log(hm.info.strUsername);

UserEntity.js:

function User() {
    this.info = {
        'strUsername': 'Hashimoro'
    };
}

module.exports.person = User;

I am creating a new UserEntity I want to access the info directly as you can see from the code

But it gives this error:

console.log(hm.info.strUsername);
                   ^

TypeError: Cannot read property 'strUsername' of undefined
    at Object.<anonymous> (D:\NodeLearn\Testing\app.js:4:20)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

If someone can give a fix and explain would be great

Thank you, seriously appreciate it

Share Improve this question edited Oct 24, 2016 at 13:41 Wexo asked Oct 24, 2016 at 13:28 WexoWexo 1751 gold badge1 silver badge9 bronze badges 6
  • What is the node version that you are using? This works for me and I have the expected result. – Stavros Zavrakas Commented Oct 24, 2016 at 13:39
  • So you have a file called app.js and another called App.js ? – InsOp Commented Oct 24, 2016 at 13:39
  • I am using the latest LTS 6.9.1, No i have only one app.js – Wexo Commented Oct 24, 2016 at 13:41
  • Works for me, too. No issue can't be seen. – CFrei Commented Oct 24, 2016 at 13:44
  • The sayHello() works, I am trying to access it directly 2 answer's have be given and awesomely explained :) – Wexo Commented Oct 24, 2016 at 13:45
 |  Show 1 more ment

4 Answers 4

Reset to default 6

This looks a little different from how Node modules are typically written.

There's a great resource on how to write them here.

You can refactor to something like this:

UserEntity.js:

function User() {
  this.info = {
    'strUsername': 'Hashimoro'
  };
}

User.prototype.getInfo = function() {
  return this.info;
};

module.exports = User;

app.js

var Person = require('./UserEntity.js');
var person = new Person();

console.log(person.info.strUsername);

// OR
console.log(person.getInfo().strUsername);

Using something es6 classes it should be like that. Be a bit more explicit about the objects that you export:

app.js

var uEntity = require('./UserEntity.js');

var hm = new uEntity.person();
console.log(hm.info.strUsername);
hm.sayHello();

UserEntity.js

'use strict';

class User {
  constructor() {
    this.info = {
      'strUsername': 'Hashimoro'
    };
  }

  sayHello () { 
    console.log(`Username is: ${this.info.strUsername}`);
  }
}

module.exports = {
  person: User
};

In my case I have not added this JSON middleware

app.use(express.json());

Return the info object also from the function.

    function User() {
        this.info = {
            'strUsername': 'Hashimoro'
        };

        return {
            sayHello: ()=> { console.log('Username is: ' + info.strUsername); }
,
info : this.info
        };
    }

    module.exports.person = User;
发布评论

评论列表(0)

  1. 暂无评论