请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

type is not defined 错误

老师你好,我在跟着视频写代码调试的过程中碰到这个问题不知道如何排查,请指教。

当我用微信订阅公众号时,所有的信息返回都正常,取消订阅的时候,报了一个type is not defined 的错误。我将content打印出来也是undefined状态。

https://img1.sycdn.imooc.com/szimg//57343a580001331607770326.jpg


报错的代码是在util.js的这一段

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

info.content = content 

info.createTime = new Date().getTime()

info.msgType = type

info.toUserName = fromUserName

info.fromUserName = toUserName


return tpl.compiled(info)

}


取消订阅号时,content为空值。




---------------继续调试---------------------------------------------------

      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)

这个地方打印出来的数据是正确的。。我继续调试。错误应该是出现在  wechat.reply.call(this)   这个函数中,调用了wechat.js中的这个函数,this.body是一个undefined ,再传入到util.js 中的tpl中报了这个错误。由于是跟着视频写的代码,不确定这部分代码是不是写错了还是什么地方没调用对。

Wechat.prototype.reply = function(){

    var content =  this.body

    var message = this.weixin

    var xml = util.tpl(content, message)


    this.status = 200

    this.tpye = 'application/xml'

    this.body = xml

}


正在回答

2回答

apple2008 2016-05-12 21:42:33

在 g.js 里面打印一下,推送过来的 xml 数据有没有被正确解析,还有没有被正确传递:


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)


0 回复 有任何疑惑可以回复我~
慕用2246375 2016-10-12 23:46:05

每次改代码必须重启服务 ,大家是不是没重启服务 所以老报错  type 问题

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号