TypeError: fs.creatReadStream is not a function 请求老师解答
来源:
一直都在路上
2016-10-18
好像别的同学没有我这个问题欸 已经引用了fs ,在这之前都没什么问题,代码如下:
/*wechat.js*/ 'use strict' var Promise = require('bluebird') var fs = require('fs') var request = Promise.promisify(require('request')) var util = require('./util') var prefix = 'https://api.weixin.qq.com/cgi-bin/' var api = { access_token: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.fetchAccessToken = function(data){ var that = this if(this.access_token && this.expires_in){ if(this.isValidAccessToken(this)){ return Promise.resolve(this) } } return this.getAccessToken() .then(function(data){ try{ data = JSON.parse(data) } catch(e){ return that.updateAccessToken() } if(that.isValidAccessToken(data)){ return Promise.resolve(data) } else{ return that.updateAccessToken() } }) .then(function(data){ that.access_token = data.access_token that.expires_in = data.expires_in that.saveAccessToken(data) return Promise.resolve(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.updateAccessToken = function(){ var appID = this.appID; var appSecret = this.appSecret; var url = api.access_token+'&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.uploadMaterial = function(type, filepath){ var that = this; console.log(fs.creatReadStream) var form = { media: fs.creatReadStream(filepath) } 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; if(_data){ resolve(_data) } else{ throw new Error('Upload material fails') } }) .catch(function(err){ reject(err) }) }) }) } Wechat.prototype.reply = function(){ var content = this.body var message = this.weixin var xml = util.tpl(content,message) this.status = 200 this.type = "application/xml" this.body = xml } module.exports=Wechat /*weixin.js*/ 'use strict' var config = require('./config') var Wechat = require('./wechat/wechat') var wechatApi = new Wechat(config.wechat) exports.reply = function*(next){ var message = this.weixin if(message.MsgType ==='event'){ if(message.Event==='subscribe'){ if(message.EventKey){ console.log('扫一扫进来的'+message.EventKey+' '+message.Ticket) } this.body = '哈哈,你订阅了\r\n' } else if(message.Event==='unsubscribe'){ console.log('取消关注了') this.body ='' } else if(message.Event==='LOCATION'){ this.body = '上报的位置是'+message.Latitude+'/'+message.Longitude+'-'+message.Precision } else if(message.Event==='CLICK'){ this.body = '点击了菜单: '+message.EventKey } else if(message.Event==='SCAN'){ console.log('关注后扫二维码'+message.EventKey+' '+message.Ticket) this.body = '看到你扫一下哦' } else if(message.Event==='VIEW'){ this.body = '您点击了菜单中的连接 '+message.EventKey } } else if(message.MsgType==='text'){ var content = message.Content var reply = '你说的'+ content+' 让我有点懵比' if(content==='1'){ reply = '星期一推荐《突然好想你》' }else if(content==='2'){ reply = '星期二推荐《兄弟》' }else if(content === '3'){ reply = '星期三推荐《如果我们不曾相遇》' }else if(content === '4'){ reply = [{ title:'技术改变世界', description:"你可以的", picUrl:'./22.jpg', url:'www.baidu.com' },{ title:'node.js开发的', description:"你可以的", picUrl:'./22.jpg', url:'www.baidu.com' }] }else if(content === '5'){ var data = yield wechatApi.uploadMaterial('image',__dirname+'./22.jpg') reply = { type:'image', mediaId:data.media_id } } this.body = reply } yield next }
写回答
1回答
-
creatReadStream
拼错了,是
createReadStream 吧
012016-10-18
相似问题