请稍等 ...
×

采纳答案成功!

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

scott老师您好,在回复消息时,提示找不到属性FromUserName,希望您能帮忙找一下问题所在

-------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);
}

-------wechat.js-------
Wechat.prototype.reply=function(){
    var content=this.body;
    var message=this.weixin;
    console.log(content+':'+message);
    var xml=util.tpl(content,message);

    this.status=200;
    this.type='application/xml';
    this.body=xml;
}

-------weixin.js-------
exports.reply=function* (next){
    var message=this.weixin;
    for(var i in message ){
        console.log(i+':'+message[i]);           //获得属性 属性值
    }

    if(message.MsgType==='event'){
        if(message.Event==='subcribe'){
            if(message.EventKey){
                console.log('扫描二维码进行关注的'+message.EventKey+' '+message.ticket);
            }
            this.body='哈哈,你订阅了这个公众号\r\n'+'消息ID:'+message.MsgId;

        }else if(message.Event==='unsubscribe'){
            console.log('无情取消了关注,呜呜呜呜');
            this.body='';
        }
    }else {

    }

    yield next;

}

-------g.js-------
module.exports = function (opts, handler) {

    var wechat = new Wechat(opts);

    return function *(next) {
        console.log(this.query);
        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') {
            if (sha !== signature) {
                this.body = 'wrong'
                return false;
            }

            var data = yield getRawBody(this.req, {
                length: this.length,
                limit: '1mb',
                encoding: this.charset
            })

            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);

            console.log(this.body+':::::'+this.weixin)
            wechat.reply().call(this);

        }
    };
}

-------app.js-------
'use strict'

var Koa=require('koa');
var path=require('path');
var util=require('./libs/util');
var wechat=require('./wechat/g');
var config=require('./config');
var weixin=require('./weixin');
var wechat_file=path.join(__dirname,'./config/wechat.txt');


var app=new Koa();

app.use(wechat(config.wechat,weixin.reply));

app.listen(1234);

console.log('Listening:1234');

ToUserName:gh_f8e7819c34d2

FromUserName:oOZxaxMCy2Tf3Npjo0KWmCtC-U1E

CreateTime:1461650916

MsgType:text

Content:放豆腐豆腐

MsgId:6277742882792053100

undefined:::::[object Object]

undefined:undefined


  TypeError: Cannot read property 'FromUserName' of undefined

      at Object.exports.tpl (/Users/william/WebstormProjects/william/wechat/wechat/util.js:54:29)

      at Wechat.reply (/Users/william/WebstormProjects/william/wechat/wechat/wechat.js:80:18)

      at Object.<anonymous> (/Users/william/WebstormProjects/william/wechat/wechat/g.js:54:20)

      at next (native)

      at Object.<anonymous> (/Users/william/node_modules/koa-compose/index.js:28:12)

      at next (native)

      at onFulfilled (/Users/william/node_modules/co/index.js:65:19)

      at process._tickCallback (node.js:412:9)



正在回答

1回答

Scott 2016-04-26 18:19:40

wechat.reply().call(this);


这句代码写错了,应该是:


wechat.reply.call(this)


1 回复 有任何疑惑可以回复我~
  • 提问者 暴力小蟑螂 #1
    现在的问题是this.body取到的content 是undefined,请指教
    回复 有任何疑惑可以回复我~ 2016-04-27 08:55:45
  • 提问者 暴力小蟑螂 #2
    非常感谢!
    回复 有任何疑惑可以回复我~ 2016-04-27 08:55:50
  • 提问者 暴力小蟑螂 #3
    node版本5.9.0
    回复 有任何疑惑可以回复我~ 2016-04-27 09:14:04
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信