Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NODEJS:搜索完成前将显示结果页面_Javascript_Asynchronous_Async Await_Spawn - Fatal编程技术网

Javascript NODEJS:搜索完成前将显示结果页面

Javascript NODEJS:搜索完成前将显示结果页面,javascript,asynchronous,async-await,spawn,Javascript,Asynchronous,Async Await,Spawn,我想查询3个数据库并在结果页面中显示结果,但此页面在查询完成之前显示 如何仅在3个查询完成后显示结果页 我尝试过承诺、回调函数、async/await和python的spawnSync,但都不起作用 任何帮助都将不胜感激 谢谢 代码如下: //Import libraries const express = require('express'); const path = require('path'); const ejs = require('ejs'); const bodyParser

我想查询3个数据库并在结果页面中显示结果,但此页面在查询完成之前显示

如何仅在3个查询完成后显示结果页

我尝试过承诺、回调函数、async/await和python的spawnSync,但都不起作用

任何帮助都将不胜感激

谢谢

代码如下:

//Import libraries
const express = require('express');
const path = require('path');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const app = express();


//Load forms
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

//Load static elements
app.use(express.static(__dirname + '/public'));

//Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.get('/', function(req,res){
    res.render('search.ejs');
});

app.post('/results', function(req,res){
    var products = req.body.products;
    var countries = req.body.countries;
    price_data = lookup('prices', products, countries);
    description_data = lookup('descriptions', products, countries);
    other_data = lookup('other', products, countries);
    table_data = get_table_data(price_data, description_data, other_data);
    res.render('results.ejs', {'table_data': table_data});


    //THIS IS THE PROBLEM. THIS PAGE IS RENDERED BEFORE THE 3 LOOKUPS ARE FINALIZED SO IT DISPLAYS NO SEARCH RESULTS



});

function lookup(type, products, countries) {
    var spawn = require('child_process').spawn;
    var py = spawn('python', [type + '_lookup.py']);
    var data = [products, countries];
    var python_output_string ='';
    py.stdin.write(JSON.stringify(data));
    py.stdin.end();
    py.stdout.on('data', function(data) {
        python_output_string = python_output_string + data;
    });
    py.stdout.on('end', function() {
        console.log(python_output_string);
        return python_output_string;
    });
}

您可以尝试在
lookup
方法中返回一个承诺并等待它们,比如(我省略了不相关的代码):

但是,我更喜欢
承诺。所有
方法:

app.post('/results', async (req, res) => {
  // ...

  Promise.all(
    lookup('prices', products, countries),
    lookup('descriptions', products, countries),
    lookup('other', products, countries)
  )
    .then(values => get_table_data(...values)) // [price_data, description_data, other_data]
    .then(table_data => res.render('results.ejs', { table_data }));
});

从查找函数返回
Promise
app.post('/results', async (req, res) => {
  // ...

  Promise.all(
    lookup('prices', products, countries),
    lookup('descriptions', products, countries),
    lookup('other', products, countries)
  )
    .then(values => get_table_data(...values)) // [price_data, description_data, other_data]
    .then(table_data => res.render('results.ejs', { table_data }));
});