采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
输入正文: TypeError: Cannot read property 'apply' of undefined
应该是这里
(config, reply)
这里 reply ,你看下引入这个文件的地方,是不是未对它传入这个外部的 reply 方法
const xml2js = require('xml2js') const template = require('./tpl') exports.parseXML = xml => { return new Promise((resolve, reject) => { xml2js.parseString(xml, {trim: true}, (err, content) => { if (err) reject(err) else resolve(content) }) }) } const formatMessage = result => { let message = {} if (typeof result === 'object') { const keys = Object.keys(result) for (let i = 0; i < keys.length; i++) { let item = result[keys[i]] let key = keys[i] if (!(item instanceof Array) || item.length === 0 ) { continue } if (item.length === 1) { let val = item[0] if (typeof val === 'object') { message[key] = formatMessage(val) } else { message[key] = (val || '').trim() } } else { message[key] = [] for (let j = 0; j < item.length; j++) { message[key].push(formatMessage(item[j])) } } } } return message } const tpl = (content, message) => { let type = 'text' if (Array.isArray(content)) { type = 'news' } if (!content) content = 'Empty News' if (content && content.type) { type = content.type } let info = Object.assign({}, { content: content, msgType: TypeError, createTime: new Date().getTime(), toUserName: message.FromUserName, FromUserName: message.ToUserName }) return template(info) } exports.formatMessage = formatMessage
const sha1 = require('sha1') const getRawBody = require('raw-body') const util = require('./util') module.exports = (config, reply) => { return async (ctx, next) => { const { signature, timestamp, nonce, echostr } = ctx.query const token = config.token let str = [token, timestamp, nonce].sort().join('') const sha = sha1(str) if (ctx.method === 'GET') { if (sha === signature) { ctx.body = echostr } else { ctx.body = 'Failed' } } else if (ctx.method === 'POST') { if (sha !== signature) { return (ctx.body = 'Failed') } const data = await getRawBody(ctx.req, { length: ctx.length, limit: '1mb', encoding: ctx.charset }) console.log(data) const content = await util.parseXML(data) console.log('content') console.log(content) const message = util.formatMessage(content.xml) console.log('message') console.log(message) ctx.weixin = message await reply.apply(ctx, [ctx, next]) const replyBody = ctx.body const msg = ctx.weixin const xml = util.tpl(replyBody, msg) console.log(xml) ctx.status = 200 ctx.type = 'application/xml' ctx.body = xml } } }
可以把这个代码贴一下么,wechat-lib/middleware.js 这里的 39 行前后,看报错,是一个参数缺少定义导致的
TypeError: Cannot read property 'apply' of undefined at /root/imooc/wechat/course4/wechat-lib/middleware.js:39:19 at processTicksAndRejections (internal/process/next_tick.js:81:5)
登录后可查看更多问答,登录/注册
从零开始, 打造智能微信公众号及网站前后台,打通两端能力共享系统底层。
854 11
803 7
854 5
798 5
1.2k 4