Cannot read property 'type' of undefined
来源:4-3 第四天 自动回复各种消息
甫里
2016-06-16
能够打印出取消关注 但是还是会报错Cannot read property 'type' of undefined
exports.tpl =function(content,message){ var info = {}; var type = 'text'; var fromUserName = message.FromUserName var toUserName = message.ToUserName; if (Array.isArray(content)) { type = 'news' } console.log(content.type) type = content.type || type; info.content = content; info.msgType = type; info.createTime = new Date().getTime() info.toUserName = fromUserName; info.fromUserName =toUserName; return tpl.compiled(info) }
//g.js 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 timestamp = this.query.timestamp; var nonce = this.query.nonce; 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') { if (sha !== signature) { return false; } var data = yield getRawBody(this.req,{ length:this.length, limit: '1mb', encoding: this.charset }) var content = yield util.parseXMLAsync(data) var message = util.formatMessage(content.xml) this.weixin = message; yield handler.call(this,next); wechat.reply.call(this) } } }
//util.js 'use strict' var Promise=require('bluebird'); var xml2js =require('xml2js'); var parser = new xml2js.Parser(); var tpl = require('./tpl') exports.parseXMLAsync = function (xml) { return new Promise(function(resolve,reject){ parser.parseString(xml,function(err,content){ if (err) reject(err) else resolve(content) }) }) } function formatMessage(result){ var message = {}; if (typeof result === 'object') { var keys = Object.keys(result); for(var i = 0; i<keys.length; i++){ var item = result[keys[i]] var key = keys[i]; if (!(item instanceof Array)||item.length ===0) { continue } if (item.length === 1) { var val = item[0]; if (typeof val === 'object') { message[key] =formatMessage(val) } else{ message[key]=(val || '').trim() } } else{ message[key]=[]; for (var j = 0, k =item.length; j<k; j++) { message[key].push(formatMessage(item[j])); } } } } return message } exports.formatMessage =formatMessage; exports.tpl =function(content,message){ var info = {}; var type = 'text'; var fromUserName = message.FromUserName var toUserName = message.ToUserName; if (Array.isArray(content)) { type = 'news' } console.log(content.type) type = content.type || type; info.content = content; info.msgType = type; info.createTime = new Date().getTime() info.toUserName = fromUserName; info.fromUserName =toUserName; return tpl.compiled(info) }
//wechat.js 'use strict' var Promise=require('bluebird'); var request=Promise.promisify(require('request')); var prefix='https://api.weixin.qq.com/cgi-bin/'; var util = require('./util') var api={ accessToken:prefix+'token?grant_type=client_credential' } function Wechat(opts){ var that = this; this.appID=opts.appID; this.appsecret=opts.appsecret; this.getAccessToken=opts.getAccessToken; this.saveAccessToken=opts.saveAccessToken; this.getAccessToken() .then(function(data){ try{ data=JSON.parse(data); } catch(e){ return that.updataAccessToken() } if (that.isValidAccessToken(data)) { return Promise.resolve(data) }else{ return that.updataAccessToken() } }) .then(function(data){ that.data=data.access_token; that.expires_in=data.expires_in; that.saveAccessToken(data) }) } Wechat.prototype.isValidAccessToken = function(data) { if (!data || !data.access_token || !data.expires_in) { return false; } var access_token=data.access_token; var expires_in=data.expires_in; var now = (new Date().getTime()) if (now < expires_in) { return true }else{ return false } }; Wechat.prototype.updataAccessToken = function() { var appID = this.appID; var appsecret = this.appsecret; var url=api.accessToken+'&appid='+appID+'&secret='+appsecret; return new Promise(function(resolve,reject){ request({url: url , json : true}).then(function(response){ var data = response.body; var now = (new Date().getTime()) var expires_in = now+(data.expires_in-20)*1000 data.expires_in = expires_in; resolve(data) }) }) } Wechat.prototype.reply = function() { var content = this.body; var message = this.weixin; console.log(content);//undefined var xml = util.tpl(content,message); this.status = 200; this.type = 'application/xml'; this.body = xml }; module.exports=Wechat;
写回答
2回答
-
Scott
2016-06-17
yield handler.call(this,next); 这里的 next 应该是传入的 handler 吧
012016-10-12 -
Scott
2016-06-16
把更多源代码贴一下,同时可以先把 content 也打印下,看看 content 是不是就是空的
032016-06-26
相似问题