为什么 wechat is not defined

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

慕粉3278383

2016-05-09

$ node --harmony app.js

Listening: 1234

{ xml:

   { ToUserName: [ 'gh_a1fa4a51afca' ],

     FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],

     CreateTime: [ '1462792816' ],

     MsgType: [ 'event' ],

     Event: [ 'unsubscribe' ],

     EventKey: [ '' ] } }

{ ToUserName: 'gh_a1fa4a51afca',

  FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',

  CreateTime: '1462792816',

  MsgType: 'event',

  Event: 'unsubscribe',

  EventKey: '' }

无情取关


  ReferenceError: wechat is not defined

      at Object.<anonymous> (C:\Users\liuhui\Desktop\wechat\wechat\g.js:59:4)

      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)

----------------------------------------------   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 方法

}

}


}




写回答

2回答

慕用2246375

2016-10-12

我解决了这个问题 ,改代码了没重启 ,无语了

0
0

Scott

2016-05-10

module.exports = function(opts, handler) {

//var wechat = new Wechat(opts)   //初试化 构造函数


wechat 被你注释掉了吧,所有后面调用的时候,这个 wechat 是未定义的

0
0

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

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

1742 学习 · 787 问题

查看课程