TypeError: Cannot read property 'type' of undefined

来源:4-3 第四天 自动回复各种消息

WiX

2016-08-13

我的content能打印
d:\wix_master\wix\imooc_wechat>node --harmony app.js                                               
listening 3000                                                                                     
{"access_token":"aJE6XXXXXXx-Qcqgsd84R06BXXXXXUW1sf49g2SXXXXXXXXXXXX0pS8pgWQMmV
UunXXXXXXXXwL5XXXXXXSR2G_SQJEHcABAXSV","expires_in":1471077914864}              
{ xml:                                                                                             
   { ToUserName: [ 'gh_036ba0d63aed' ],                                                            
     FromUserName: [ 'ohHV4xBZ2VZQ4xEA5wv21KhnrrOg' ],                                             
     CreateTime: [ '1471077297' ],                                                                 
     MsgType: [ 'text' ],                                                                          
     Content: [ '哈哈' ],                                                                          
     MsgId: [ '6318228880921866460' ] } }                                                          
{ ToUserName: 'gh_036ba0d63aed',                                                                   
  FromUserName: 'ohHV4xBZ2VZQ4xEA5wv21KhnrrOg',                                                    
  CreateTime: '1471077297',                                                                        
  MsgType: 'text',                                                                                 
  Content: '哈哈',                                                                                 
  MsgId: '6318228880921866460' }                                                                   
微信的Messge : [object Object]                                                                     
                                                                                                   
  TypeError: Cannot read property 'type' of undefined                                              
      at Object.exports.tpl (D:\wix_master\wix\imooc_wechat\wechat\util.js:65:19)                  
      at Object.Wechat.reply (D:\wix_master\wix\imooc_wechat\wechat\wechat.js:82:20)               
      at Object.<anonymous> (D:\wix_master\wix\imooc_wechat\wechat\g.js:57:26)                     
      at next (native)                                                                             
      at Object.<anonymous> (D:\wix_master\wix\imooc_wechat\node_modules\koa-compose\index.js:28:19
)                                                                                                  
      at next (native)                                                                             
      at onFulfilled (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:65:19)               
      at process._tickCallback (internal/process/next_tick.js:103:7)



util.js:65

exports.tpl = function(content,message) {
    var info = {}
    var type = 'text'
    var fromUserName = message.FromUserName
    var toUserName = message.toUserName

    if (Array.isArray(content)) {
        type = 'news'
    }

    type = content.type || type        //《===65行
    info.content = content
    info.createTime = new Date().getTime()
    info.msgType = type
    info.toUserName = fromUserName
    info.formatMessage = toUserName

    return tpl.compiled(info)

}


wechat.js

Wechat.prototype.reply = function() { 
    var content = this.body
    var message = this.weixin
    var xml = util.tpl(content, message)            //《==82行

    this.status = 200
    this.type = 'application/xml'
    this.body = xml
}

g.js

module.exports = function(opts, handler) { //实例Wechat处理票据检查,票据更新,opts是appjs传入的config
    var wechat = new Wechat(opts)

…………………………………………

            //console.log(data.toString())
            var content = yield util.parseXMLAsync(data)

            console.log(content)

            var message = util.formatMessage(content.xml)
            console.log(message)

            this.weixin = message

            yield handler.call(this, next)

            wechat.reply.call(this)                     //<==57行
        }
    }
}


写回答

2回答

Scott

2016-08-13

type = content.type || type

前面加一个合理化处理:


content = content || {}


但感觉是 reply js 里面没有把代码写对,导致传进来的 content 是 undefined,所以报错

1
0

WiX

提问者

2016-08-13

完整bash:

                                                                                                   
  TypeError: You may only yield a function, promise, generator, array, or object, but the following
 object was passed: "undefined"                                                                    
      at next (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:101:25)                     
      at onFulfilled (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:69:7)                
      at D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:54:5                              
      at Object.co (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:50:10)                 
      at Object.toPromise (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:118:63)         
      at next (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:99:29)                      
      at onFulfilled (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:69:7)                
      at D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:54:5                              
      at Object.co (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:50:10)                 
      at Object.toPromise (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:118:63)         
      at next (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:99:29)                      
      at onFulfilled (D:\wix_master\wix\imooc_wechat\node_modules\co\index.js:69:7)                
      at tryCatcher (D:\wix_master\wix\imooc_wechat\node_modules\bluebird\js\release\util.js:16:23)
                                                                                                   
      at Promise._settlePromiseFromHandler (D:\wix_master\wix\imooc_wechat\node_modules\bluebird\js
\release\promise.js:504:31)                                                                        
      at Promise._settlePromise (D:\wix_master\wix\imooc_wechat\node_modules\bluebird\js\release\pr
omise.js:561:18)                                                                                   
      at Promise._settlePromiseCtx (D:\wix_master\wix\imooc_wechat\node_modules\bluebird\js\release
\promise.js:598:10)



0
2
Apieceofcake
遇到一样的问题,兄弟怎么解决的?
2018-06-09
共2条回复

7天搞定Node.js微信公众号

Koa框架、ES2015新特性、MongoDB,开发微信公众号

1742 学习 · 787 问题

查看课程