请稍等 ...
×

采纳答案成功!

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

Cannot read property 'forEach' of undefined

1
2
3
4
5
6
7
8
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 + '--------');

https://img1.sycdn.imooc.com/szimg//581aad9d0001a0c205830071.jpg

scott老师好:

我这里一直报这个错误,我分别打印了data和items,data是[object Object],items不知道为啥是undefined,我也看了公众号上的文档:

https://img1.sycdn.imooc.com/szimg//581aae670001ce6603380343.jpg

data.news_item应该是一个数组哇。。。这里卡了半天了,实在找不错误来

正在回答 回答被采纳积分+3

插入代码

3回答

Scott 2016-11-03 13:02:43

你不要加 +'----' 这些后缀,就直接打印干净的变量看看

0 回复 有任何疑惑可以回复我~
  • 提问者 慕名小白 #1
    我把后缀去掉后,打印出40007的错误(不合法的媒体文件id),然后我打印了data.media_id,显示undefined,这样的话,后面fetch的时候肯定是不合法的吧,但是为啥data.media_id是undefined呢?这里没法贴代码,我把代码截图贴在下一层评论了
    回复 有任何疑惑可以回复我~ 2016-11-03 13:24:31
  • 提问者 慕名小白 #2
    还有就是,根据后续的课程,获取素材列表的时候,用JSON.stringify(results)的结果,发现,打印了好多重复的的content===“10”的那个数组列表,说明上传上去了,但是fetch没有获取到,代码没有发现错误哇。。。有点晕
    回复 有任何疑惑可以回复我~ 2016-11-03 14:17:02
提问者 慕名小白 2016-11-03 13:34:01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
这是上传素材和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);
                }  
            })
    })
}


0 回复 有任何疑惑可以回复我~
提问者 慕名小白 2016-11-03 13:29:07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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;
}

https://img1.sycdn.imooc.com/szimg//581aca9f00019ae505550103.jpg

其实我在9的时候,有这样的问题:https://img1.sycdn.imooc.com/szimg//581acae1000126eb06190048.jpg

超过了接口调用限制,是不是这个原因,导致我的饿素材上传不上去,所以获取的时候,就是undefined哇

0 回复 有任何疑惑可以回复我~
  • Scott #1
    对,单日打到上限了,第二天再试试看
    回复 有任何疑惑可以回复我~ 2016-11-03 16:50:35
  • 提问者 慕名小白 回复 Scott #2
    谢谢老师,耽误中午的休息时间给我解答,嘿嘿!!
    回复 有任何疑惑可以回复我~ 2016-11-03 16:56:41
  • 提问者 慕名小白 回复 Scott #3
    谢谢老师,耽误中午的休息时间给我解答,嘿嘿!!
    回复 有任何疑惑可以回复我~ 2016-11-03 16:59:03
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号