node.js - Nodejs报错Cannot read property 'call' of undefined
PHP中文网
PHP中文网 2017-06-29 10:09:20
[Node.js讨论组]

在中间件中这么写:
wechat.reply.call(this);
在wechat中的reply函数是这么写的:

Wechat.prototype.reply = function(){
    console.log('bbbbb');
    var content = this.body;
    var message = this.weixin;
    var xml = util.tpl(content,message)

    console.log(xml);
    this.status = 200
    this.type = 'application/xml'
    this.body = xml
    return
}

然后就报错

TypeError: Cannot read property 'call' of undefined
      at Object.<anonymous> (C:\www\koa\wechat\wechat\g.js:54:16)
      at Generator.next (<anonymous>)
      at onFulfilled (C:\www\koa\wechat\node_modules\co\index.js:65:19)
      at process._tickCallback (internal/process/next_tick.js:109:7)
      

话说,call函数不是在原生js中就能用吗,为什么我在调用的时候报错了呢?

而且,百度了一下,有这种错误的人特别少……
问大神:通常是什么时候会报这个错?报错的原因是什么?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
大家讲道理

举个栗子 a.call(this,null),但是此时 aundefined,就会报你遇到的错误
确认有 wechat.apply 这个方法么

女神的闺蜜爱上我

在中间件中这么写:
wechat.reply.call(this);在wechat中的reply函数是这么写的:

wechat实例应该没有值,你在调用之前打印一下看看。

console.log(wechat.reply)
怪我咯

如果x.callx未定义,则其下的.call无法访问,所以你先确定一下wechat.reply是否已经定义。
另外,注意在JS中,变量名是区分大小写的。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号