Cannot read property 'forEach' of undefined
来源:4-7 第四天 荡平永久素材接口-查询永久素材数量接口、获取永久素材列表接口
慕名小白
2016-11-03
data = yield wechatApi.uploadMaterial('news', media, {}); data = yield wechatApi.fetchMaterial(data.media_id, 'news', {}); console.log(data + '+++++++'); var items = data.news_item; //news_item图文数组看微信api var news = []; console.log(items + '--------');
scott老师好:
我这里一直报这个错误,我分别打印了data和items,data是[object Object],items不知道为啥是undefined,我也看了公众号上的文档:
data.news_item应该是一个数组哇。。。这里卡了半天了,实在找不错误来
写回答
3回答
-
Scott
2016-11-03
你不要加 +'----' 这些后缀,就直接打印干净的变量看看
022016-11-03 -
慕名小白
提问者
2016-11-03
这是上传素材和fetch获取的方法 //material:如果是图文,就是数组;如果是图片或者是视频,就是字符串类型的路径 Wechat.prototype.uploadMaterial = function(type, material, permanent) { //第二个参数是素材,只要传了第三个参数,说明就是永久素材 var that = this; var form = {}; var uploadUrl = api.temporary.upload; if(permanent) { uploadUrl = api.permanent.upload; _.extend(form, permanent); //想让form兼容到所有的上传类型,包括图文消息 } if(type === 'pic') { uploadUrl = api.permanent.uploadNewsPic; } if(type === 'news') { uploadUrl = api.permanent.uploadNews; form = material; } else { form.media = fs.createReadStream(material); } return new Promise(function(resolve, reject) { that.fetchAccessToken() .then(function(data) { var url = uploadUrl + 'access_token=' + data.access_token; if(!permanent) { url += '&type=' + type; } else { form.access_token = data.access_token; } var options = { method: 'POST', url: url, json: true } if(type === 'news') { options.body = form; } else { options.formData = form; } request(options).then(function(response) { var _data = response.body; //response.body if(_data) { resolve(_data); } else { throw new Error('Upload material fails') } }) .catch(function(err) { reject(err); }) }) }) } //获取素材url Wechat.prototype.fetchMaterial = function(mediaId, type, permanent) { //第二个参数是类型,只要传了第三个参数,说明就是永久素材 var that = this; var fetchUrl = api.temporary.fetch; if(permanent) { fetchUrl = api.permanent.fetch; //获取资源的url地址 } return new Promise(function(resolve, reject) { that.fetchAccessToken() .then(function(data) { var url = fetchUrl + 'access_token=' + data.access_token; var options = {method: 'POST', url: url, json: true} var form = {}; if(permanent) { form.media_id = mediaId; form.access_token = data.access_token; options.body = form; } else { if(type === 'video') { url = url.replace('https://','http://'); } url += '&media_id=' + mediaId; } if(type === 'news' || type === 'video') { request(options).then(function(response) { //请求对应的素材数据 var _data = response.body; //response.body if(_data) { resolve(_data); } else { throw new Error('Fetch material fails') } }) .catch(function(err) { reject(err); }) } else { resolve(url); } }) }) }
00 -
慕名小白
提问者
2016-11-03
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); //ticket换取二维码图片 } console.log('你订阅了这个号'); this.body = '哈哈,你订阅了这个号'; console.log(this.body); } 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; //eventkey就是菜单的url地址 } } else if(message.MsgType === 'text') { var content = message.Content; var reply = '额,你说的' + message.Content + '太复杂了'; if(content === '1') { reply = '天下第一!'; } else if(content === '2') { reply = '天下第二!!'; } else if(content === '3') { reply = '天下第三!!!'; } else if(content === '4') { reply = [{ title: '技术改变世界', description: '只是个描述而已', picUrl: 'http://res.cloudinary.com/moveha/image/upload/v1441184110/assets/images/Mask-min.png', url: 'http://www.baidu.com' }] } else if(content === '5') { var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg'); reply = { type: 'image', mediaId: data.media_id } } else if(content === '6') { var data = yield wechatApi.uploadMaterial('video', __dirname + '/6.mp4'); reply = { type: 'video', title: '回复视频内容', description: '打篮球运动喽!', mediaId: data.media_id } } else if(content === '7') { var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg'); reply = { type: 'music', title: '回复音乐内容', description: '网易云音乐,精品好听的音乐', musicUrl: 'http://mpge.5nd.com/2015/2015-9-12/66325/1.mp3', thumbMediaId: data.media_id } } else if(content === '8') { var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg', {type: 'image'}); reply = { type: 'image', mediaId: data.media_id } } else if(content === '9') { var data = yield wechatApi.uploadMaterial('video', __dirname + '/6.mp4', {type: 'video', description: '{"title": "Really a nice place","introduction":"Never think it so easy"}'}); console.log(data); reply = { type: 'video', title: '回复视频内容', description: '打篮球运动喽!', mediaId: data.media_id } } else if(content === '10') { var picData = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg', {}); //有了第三个参数,说明是是永久素材,为了拿到图片的素材id,为了上传图文 var media = { articles: [{ title: 'lol', thumb_media_id: picData.media_id, author: 'lrn', digest: '没有摘要', show_cover_pic: 1, content: '没有内容', content_source_url: 'http://www.baidu.com'//阅读全文的url地址 },{ title: 'tututu5', thumb_media_id: picData.media_id, author: 'my', digest: '没有摘要', show_cover_pic: 1, content: '没有内容', content_source_url: 'https://github.com' }] } data = yield wechatApi.uploadMaterial('news', media, {}); console.log(data.media_id); data = yield wechatApi.fetchMaterial(data.media_id, 'news', {}); console.log(data); var items = data.news_item; //news_item图文数组看微信api var news = []; console.log(items); items.forEach(function(item) { news.push({ title: item.title, description: item.digest, picUrl: picData.url, url: item.url }) }) reply = news; } this.body = reply; } yield next; }
其实我在9的时候,有这样的问题:
超过了接口调用限制,是不是这个原因,导致我的饿素材上传不上去,所以获取的时候,就是undefined哇
032016-11-03
相似问题