亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

寫完后運行報錯

TypeError: the promise constructor cannot be invoked directly

??? See http://goo.gl/MqrFmX

??? at check (E:\imooc\http\node_modules\bluebird\js\release\promise.js:62:15)
??? at Array.Promise (E:\imooc\http\node_modules\bluebird\js\release\promise.js:72:9)
??? at Object.<anonymous> (E:\imooc\http\promise_crawler.js:94:6)
??? at Module._compile (module.js:570:32)
??? at Object.Module._extensions..js (module.js:579:10)
??? at Module.load (module.js:487:32)
??? at tryModuleLoad (module.js:446:12)
??? at Function.Module._load (module.js:438:3)
??? at Module.runMain (module.js:604:10)
??? at run (bootstrap_node.js:394:7)
??? at startup (bootstrap_node.js:149:9)
??? at bootstrap_node.js:509:3

想知道為什么不能調用

正在回答

3 回答

你這代碼里面大量語法錯誤,很多變量沒有var ,而且沒用;用的,

0 回復 有任何疑惑可以回復我~
#1

舊人舊城那點事兒

可以采用鏈式聲明變量。
2018-06-18 回復 有任何疑惑可以回復我~

最下面一部分 promise.all 不是promise.call額。。。。

0 回復 有任何疑惑可以回復我~

var http = require('http');
var cheerio = require('cheerio');
var Promise = require('bluebird');
var baseUrl = 'http://www.xianlaiwan.cn/learn/';
var url = 'http://www.xianlaiwan.cn/learn/348';
var videIds = [728, 637, 348, 259, 197, 134, 75];

function filterChapters(html) {
?var $ = cheerio.load(html),
??chapters = $('.chapter'),
??????? title = $('.hd .cleadrfix h2').text();
??????? number = $('.meta-value .js-learn-num').text();
?
??courseData = {
??????????? title: title,
??????????? number: number,
??????????? videos : []
??????? }
?
?chapters.each(function (item) {
??var chapter = $(this),
???chapterTitle = chapter.find('strong').text(),
???videos = chapter.find('.video').children('li'),
???chapterData = {
????chapterTitle: chapterTitle,
????videos: []
???};
??videos.each(function (item) {
???var video = $(this).find('.J-media-item'),
????videoTitle = video.text(),
????id = video.attr('href').split('video/')[1];
???
???chapterData.videos.push({
????title: videoTitle,
????id: id
???});
??});
??
??courseData.videos.push(chapterData);
?});
?
?return courseData;
}

function printCourseInfo(coursesData) {
??? coursesData.forEach(function(courseData)? {
??????? console.log(courseData.number + '人學過' + couresData.title + '\n');
??? })
???
?coursesData.forEach(function (coursesData) {
??????? console.log('****' + courseData.title + '\n');
??????? courseData.videos.forEach(function(item) {
??????????? var chapterTitle = item.chapterTitle;

??????????? console.log(chapterTitle + '\n');
??????????? item.videos.forEach(function (video) {
??????????????? console.log('?? [' + video.id + ']' + video.title + '\n');
??????????? });
??????? })
??
?});
}

function getPageAsync(url) {
??? return new Promise(function(resolve,reject) {
??????? console.log('正在爬取' + url);
???????
??????? http.get(url, function (res) {
??????????? var html = '';
??????????? res.on('data', function (data) {
??????????????? html += data;
??????????? });
???????????
??????????? res.on('end', function () {
???????????????
??????????????? resolve(html);

???????????????
??????????? });
??????? }).on('error', function (e) {
???????????
??????????? reject(e)
??????????? console.log('獲取課程數據出錯');
??????? });
??? })
}

var fetchCourseArray = [];
videIds.forEach(function(id) {
??? fetchCourseArray.push(getPageAsync(baseUrl + id))
})

Promise
??? .call(fetchCourseArray)
??? .then(function(pages) {
??????? var coursesData = [];
???
???
??????? pages.forEach(function(html) {
??????????? var courses = filterChapters(html);
??????????? coursesData.push(courses);
??????? })
??????? coursesData.sort(function(a, b) {
??????????? return a.number < b.number;
??????? })
???????
??????? printCourseInfo(courseData);
??? })


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
進擊Node.js基礎(二)
  • 參與學習       76735    人
  • 解答問題       242    個

本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務器端

進入課程

寫完后運行報錯

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號