私はindex.jsにrouter.postを持っており、ユーザーからの入力を取得してからquestion.ejsという別のページにユーザーを誘導しようとしています。 Question.ejsには、webからユーザーの関連記事をスクラップしてhtmlで表示するquestion.jsというjsがあります。問題は、リクエストが完了する前に私のquestion.ejsが読み込まれ、表示するコンテンツがないことです。もう一つの奇妙なことは、私はすべてを動作させるために2回Enterキーを押す必要があります...説明するのは非常に難しいですが、私は自分の問題のビデオを作りました。res.renderはリクエスト機能がロードされるのを待っていません
私はいくつかのコンソールログを作成しました。彼らは何を言っていて、の隣に数字が表示されていますは端末のコンソールログの順序を示します。ここで
はindex.jsのための私のコードです:ここでは
var express = require('express');
var router = express.Router();
var news = require('../news.json');
var newsQuestion = require('../newsQuestion.json');
var bodyParser = require('body-parser');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', {
title: 'Express',
news: news
});
});
var parse = require('./parse');
router.use(bodyParser.urlencoded({ extended: true }));
router.post('/questionUser', function(req,res){
newsQuestion.question.txt = req.body.id;
//var question = require('./question');
question = require('./question');
router.use('/question',question);
console.log("this is after router.use: ", newsQuestion.bbc.title); =>
3**undefined**
res.redirect('question');
});
module.exports = router;
はここquestion.js
var express = require('express');
var router = express.Router();
var news = require('../news.json');
var newsQuestion = require('../newsQuestion.json');
var bodyParser = require('body-parser');
var request = require('request');
var cheerio = require('cheerio');
var linkName = 'http://www.bbc.co.uk/search?
q='+newsQuestion.question.txt+'&sa_f=search-product&filter=news&suggid=';
console.log('this is before request: ', newsQuestion.bbc.title); =>
1**undefined**
request(linkName, function (error, response, html) {
console.log('This is inside of request but before the actual
scrapping'); => 4**undefined**
var titleArray = [];
var linkArray = [];
var img = [];
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('a', 'h1').each(function() {
var title = $(this).text().trim();
var link = $(this).attr('href');
if (title.length != 0) {
titleArray.push(title);
linkArray.push(link);
}
});
$('img').each(function() { //#siteTable is a the context and a.title is within the context.
img.push($(this).attr('src'));
});
console.log('This is inside request: ', newsQuestion.bbc.title); => 5*undefined*
newsQuestion.bbc.imgUrl = img[0];
newsQuestion.bbc.title = titleArray[0];
newsQuestion.bbc.url = linkArray[0];
console.log(newsQuestion.bbc.title); => 6*Chile moves towards legalizing abortion in limited cases*
}
});
console.log('this is after request: ',newsQuestion.bbc.title); => 2**undefined**
router.get('/', function(req, res, next) {
res.render('question');
});
ための私のコードで私のターミナルれる:
"C:\Program Files\JetBrains\WebStorm 2017.1.4\bin\runnerw.exe" "C:\Program
Files\nodejs\node.exe" "C:\Users\Archie
Jugdersuren\WebstormProjects\summaproject_trial3 - Copy (3)\bin\www"
this is inside parse
GET/304 9.783 ms - -
GET /stylesheets/style.css 304 1.505 ms - -
this is before request: undefined
this is after request: undefined
this is after router.use: undefined
POST /questionUser 302 14.087 ms - 60
GET /question 304 1.673 ms - -
GET /stylesheets/style.css 304 0.417 ms - -
This is inside of request but before the actual scrapping
This is inside request: undefined
Chile moves towards legalising abortion in limited cases
私のビデオはこちら私の問題(HD)のビング:
https://www.youtube.com/watch?v=y1VSu1DylFQ
ありがとうございました。