Cannot read property 'then' of undefined

来源:4-4 第四天 上传临时素材之图片和视频

甫里

2016-06-18

'use strict'

var Promise=require('bluebird');
var request=Promise.promisify(require('request'));
var prefix='https://api.weixin.qq.com/cgi-bin/';
var fs = require('fs')
var util = require('./util')
var api={
	accessToken:prefix+'token?grant_type=client_credential',
	upload:prefix+'media/upload?'
}


function Wechat(opts){
	var that = this;
	this.appID=opts.appID;
	this.appsecret=opts.appsecret;
	this.getAccessToken=opts.getAccessToken;
	this.saveAccessToken=opts.saveAccessToken;

	this.fetchAccessToken()
}

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.fetchAccessToken = function() {
	var that = this;
	if (this.access_token && this.expires_in) {
		if (this.isValidAccessToken(this)) {
			return Promise.resolve(this)
		}
	}

	that.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.access_token=data.access_token;
			that.expires_in=data.expires_in;
			that.saveAccessToken(data);
			
			return Promise.resolve(data);
		})

}
Wechat.prototype.uploadMaterial = function(type,filepath) {
	console.log(filepath)
	var form ={
		media:fs.createReadStream(filepath)
	}
	var that = this;
	
	  return new Promise(function(resolve, reject) {
        that.fetchAccessToken()
            .then(function(data) {
                var url = api.upload + 'access_token=' + data.access_token + '&type=' + type;
                request({ method: 'POST', url: url, formData: form, json: true }).then(function(response) {
                        var _data = response.body;
                        console.log(_data)
                        if (_data) {
                            resolve(_data);
                        } else {
                            throw new Error("Upload meterial fails");
                        }
                    })
                    .catch(function(err) {
                        reject(err)
                    })
 
            })
    })
}

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)
	var xml = util.tpl(content,message);
	this.status = 200;
	this.type = 'application/xml';
	this.body = xml
};
module.exports=Wechat;

不知道哪里错了~一直找不出来

写回答

4回答

甫里

提问者

2016-06-18

已经完美解决了~

1
1
Away0x
请问是怎么回事呢?我也是这个问题
2016-06-28
共1条回复

qq_菜花_0

2017-03-23

求解决方案........

0
0

昌隆

2016-07-16

666

0
0

甫里

提问者

2016-06-29

 return  that.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.access_token=data.access_token;

            that.expires_in=data.expires_in;

            that.saveAccessToken(data);

             

            return Promise.resolve(data);

        })


0
0

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

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

1742 学习 · 787 问题

查看课程