为什么 'type'无法读取
来源:5-4 第五天 微信菜单-生成菜单
慕粉3278383
2016-05-15
为什么 点击 点击事件会出现 Cannot read property 'type' of undefined
node --harmony app.js
Listening: 1234
{ errcode: 0, errmsg: 'ok' }
{ xml:
{ ToUserName: [ 'gh_a1fa4a51afca' ],
FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],
CreateTime: [ '1463300037' ],
MsgType: [ 'event' ],
Event: [ 'CLICK' ],
EventKey: [ 'menu_click' ] } }
{ ToUserName: 'gh_a1fa4a51afca',
FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',
CreateTime: '1463300037',
MsgType: 'event',
Event: 'CLICK',
EventKey: 'menu_click' }
TypeError: Cannot read property 'type' of undefined
at Object.exports.tpl (C:\Users\liuhui\Desktop\wechat\wechat\util.js:70:16
)
at Object.Wechat.reply (C:\Users\liuhui\Desktop\wechat\wechat\wechat.js:10
32:17)
at Object.<anonymous> (C:\Users\liuhui\Desktop\wechat\wechat\g.js:59:17)
at next (native)
at Object.<anonymous> (C:\Users\liuhui\Desktop\wechat\node_modules\koa-com
pose\index.js:28:19)
at next (native)
at onFulfilled (C:\Users\liuhui\Desktop\wechat\node_modules\co\index.js:65
:19)
at process._tickCallback (internal/process/next_tick.js:103:7)
点击弹出系统拍照或相册上传照片时不了... 也出现
Cannot read property 'type' of undefined
at Object.exports.tpl
node --harmony app.js
Listening: 1234
{ errcode: 0, errmsg: 'ok' }
{ xml:
{ ToUserName: [ 'gh_a1fa4a51afca' ],
FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],
CreateTime: [ '1463300201' ],
MsgType: [ 'event' ],
Event: [ 'pic_photo_or_album' ],
EventKey: [ 'pic_photo_album' ],
SendPicsInfo: [ [Object] ] } }
{ ToUserName: 'gh_a1fa4a51afca',
FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',
CreateTime: '1463300201',
MsgType: 'event',
Event: 'pic_photo_or_album',
EventKey: 'pic_photo_album',
SendPicsInfo: { Count: '1', PicList: { item: [Object] } } }
{ item: { PicMd5Sum: 'a66c0a1a00ee968a71062c8cbb669719' } }
1
您点击了菜单中: pic_photo_album
{ xml:
{ ToUserName: [ 'gh_a1fa4a51afca' ],
FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],
CreateTime: [ '1463300201' ],
MsgType: [ 'image' ],
PicUrl: [ 'http://mmbiz.qpic.cn/mmbiz/gVXjzuYWiaxKgQnHLnL1bibSOQdtaicn1ha5L
8gibYx3VL0ztTmnUl8rFCcy08tCo6cdrr0QpDbiaPOCtdesxXmmKQQ/0' ],
MsgId: [ '6284826507935546992' ],
MediaId: [ 'HpsjyipKwN54o1d4q72QRka3TfZuKKB3S7cLE7ytyzmu2hiYBtP2nobkKso2mOl
a' ] } }
{ ToUserName: 'gh_a1fa4a51afca',
FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',
CreateTime: '1463300201',
MsgType: 'image',
PicUrl: 'http://mmbiz.qpic.cn/mmbiz/gVXjzuYWiaxKgQnHLnL1bibSOQdtaicn1ha5L8gibY
x3VL0ztTmnUl8rFCcy08tCo6cdrr0QpDbiaPOCtdesxXmmKQQ/0',
MsgId: '6284826507935546992',
MediaId: 'HpsjyipKwN54o1d4q72QRka3TfZuKKB3S7cLE7ytyzmu2hiYBtP2nobkKso2mOla' }
undefined
TypeError: Cannot read property 'type' of undefined
at Object.exports.tpl (C:\Users\liuhui\Desktop\wechat\wechat\util.js:70:16
)
at Object.Wechat.reply (C:\Users\liuhui\Desktop\wechat\wechat\wechat.js:10
32:17)
at Object.<anonymous> (C:\Users\liuhui\Desktop\wechat\wechat\g.js:59:17)
at next (native)
at Object.<anonymous> (C:\Users\liuhui\Desktop\wechat\node_modules\koa-com
pose\index.js:28:19)
at next (native)
at onFulfilled (C:\Users\liuhui\Desktop\wechat\node_modules\co\index.js:65
:19)
at process._tickCallback (internal/process/next_tick.js:103:7)
exports.tpl = function(content, message) { //暴露出tpl var info = {} var type = 'text' var fromUserName = message.FromUserName var toUserName = message.ToUserName if (Array.isArray(content)) { type = 'news' } console.log(content) type = content.type || type info.content = content info.createTime = new Date().getTime() info.msgType = type info.toUserName = fromUserName info.fromUserName = toUserName return tpl.compiled(info) }
console.log(content) 输出 undefined
7回答
-
Scott
2016-06-23
我下载源代码,把端口号换成 1234,跑起来服务,然后通过 QQ 浏览器做了服务器代理,重新到测试号里,配置了一下代理后的地址,回复功能,点击事件,弹出相册都 ok 的,代码没问题。
我 Nodejs 的版本是:v4.2.3
032016-08-28 -
Scott
2016-06-21
嗯,看打印的日志,流程上各处的执行都没问题,应该是哪个方法名或者函数调用出问题了,把你全部的代码都贴一遍出来吧,我肉眼过一遍看看
012016-06-22 -
蜡笔小贤
2016-08-27
我研究出来了。应该是因为reply.js里面的this.body没有值。需要在if ..... 后面加一个else{ this.body = '***'; }
342017-03-30 -
甫里
2016-06-24
我也遇到了这个问题 是不是微信返回的XML数据包改变了,我console的message 没有Event,于是就reply.js 就不能判断类型了~ 自然没有this.body 没有type 了~ 我看了上面同学的代码好像也是没有Event
您点击了菜单中: pic_photo_album { xml: { ToUserName: [ 'gh_a1fa4a51afca' ], FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ], CreateTime: [ '1463300201' ], MsgType: [ 'image' ], PicUrl: [ 'http://mmbiz.qpic.cn/mmbiz/gVXjzuYWiaxKgQnHLnL1bibSOQdtaicn1ha5L 8gibYx3VL0ztTmnUl8rFCcy08tCo6cdrr0QpDbiaPOCtdesxXmmKQQ/0' ], MsgId: [ '6284826507935546992' ], MediaId: [ 'HpsjyipKwN54o1d4q72QRka3TfZuKKB3S7cLE7ytyzmu2hiYBtP2nobkKso2mOl a' ] } } { ToUserName: 'gh_a1fa4a51afca', FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0', CreateTime: '1463300201', MsgType: 'image', PicUrl: 'http://mmbiz.qpic.cn/mmbiz/gVXjzuYWiaxKgQnHLnL1bibSOQdtaicn1ha5L8gibY x3VL0ztTmnUl8rFCcy08tCo6cdrr0QpDbiaPOCtdesxXmmKQQ/0', MsgId: '6284826507935546992', MediaId: 'HpsjyipKwN54o1d4q72QRka3TfZuKKB3S7cLE7ytyzmu2hiYBtP2nobkKso2mOla' } undefined
00 -
杰克大叔
2016-06-21
跟你遇到的问题一样,这节课遇到了两个问题,楼主解决了吗
00 -
慕粉3278383
提问者
2016-05-15
g.js
'use strict' var sha1 = require('sha1') var getRawBody = require('raw-body') var Wechat = require('./wechat') var util = require('./util') module.exports = function(opts, handler) { var wechat = new Wechat(opts) //初试化 构造函数 return function *(next) { //加密的逻辑 var that = this var token = opts.token var signature = this.query.signature var nonce = this.query.nonce var timestamp = this.query.timestamp var echostr = this.query.echostr var str = [token, timestamp, nonce].sort().join('') var sha = sha1(str) if (this.method === 'GET') { if (sha === signature) { this.body = echostr + '' } else { this.body = 'wrong' } } else if (this.method === 'POST') { //post过来的数据 if (sha !== signature) { this.body = 'wrong' return false } var data = yield getRawBody(this.req, { //可以把this 上面的request对象,http中request对象,去拼接它的数据,最终可得到buff的 XML 数据 length: this.length, //post过来的数据的长度 limit: '1mb', //限制数据体积 1兆 encoding: this.charset // 编码 }) //console.log(data.toString()) //异步请求 看xml数据 var content = yield util.parseXMLAsync(data) //工具包 把原始的xml数据 console.log(content) var message = util.formatMessage(content.xml) console.log(message) this.weixin = message yield handler.call(this, next) wechat.reply.call(this) //调用wechat 方法 } } }
00 -
Scott
2016-05-15
把 g.js 代码贴一下,看看 content 传进来以前,接收到的 xml 数据是如何的,处理的过程有没有问题
072016-08-27
相似问题