2017-08-17 81 views
0

我想查詢數據庫中,我對工作項目的快速和MySQL服務器響應用於教學和培訓用戶。我有根據用戶(例如醫生)的兩個型諾言,一個詢問的問題(和它們的question_ids)的數據庫,然後接下來的查詢需要的questions_ids,並選擇相應的答案和answer_ids的問題。代碼如下所示:不發送所有需要的數據

var express = require('express'); 
var mysql = require('mysql'); 
var path = require('path'); 
var async = require('async') 

var app = express(); 

var connection = mysql.createConnection({ 
    host: "localhost", 
    user: "root", 
    password: "123456", 
    database: "myDB" 
}) 

app.set('port', 3001); 

var questions = []; 
var question_ids = []; 
var answers = []; 
var answer_ids = [] 

app.get('/training/:user_type', function (req, res) { 
    var user_type = req.params.user_type // gets the value for the named parameter user_id from the url 
    var sql_command_1 = "SELECT * FROM questions WHERE type = ?" 
    var sql_command_2 = "SELECT * FROM answers WHERE question_id = ?" 

    function getQuestions() { 
     return new Promise((resolve, reject) => { 
      connection.query(sql_command_1, user_type, function (error, results) { 
       for (i = 0; i < results.length; i++) { 
        questions.push(results[i].question_text); 
        question_ids.push(results[i].question_id); 
       } 
       resolve({questions: questions, question_ids: question_ids}); 
      }) 
     }) 
    } 

    function getAnswers(question_ids) { 
     console.log(question_ids) 
     return new Promise((resolve, reject) => { 
      for (i = 0; i < question_ids.length; i++) { 
       connection.query(sql_command_2, question_ids[i], function (error, results) { 
        for (i = 0; i < results.length; i++) { 
         answers.push(results[i].answer_text); 
         answer_ids.push(results[i].answer_id); 
        } 
        console.log(answers); 
        console.log(answer_ids); 
        resolve({answers: answers, answer_ids: answer_ids}); 
       }); 
      } 
     }); 
    } 


    getQuestions().then((result) => { 
     console.log(result); 
     return getAnswers(result.question_ids); 
    }).then((result) => { 
     res.json({"Type": user_type, "Questions": questions, "Answers": answers}); 
    }); 
}); 

app.listen(app.get('port')); 

我預期的響應是醫生爲USER_TYPE,對於「問題」等8個答案在「答案」兩個不同的問題(四個答案爲每個不同的問題)。不過,我只得到了第一個問題,不是四個第二的四個答案。我看過了代碼,看着異步執行,但似乎無法找到我的問題。

回答

0

好吧,

我將解釋什麼是你的代碼錯誤,但首先我想建議你另一種方法來構建應用程序 - 在單個查詢

SELECT * FROM questions q 
LEFT JOIN answers a ON q.question_id = a.question_id 
WHERE q.type = ? 
獲得的所有數據

SQL會給你8條作爲響應看起來像:

question_id | type | question | answer_id | answer | .... all columns 
1    doctor question?  3  a  ....... 
1    doctor question?  4  b  ....... 
1    doctor question?  5  c  ....... 
1    doctor question?  6  d  ....... 
2    doctor question2?  11  ff  ....... 
2    doctor question2?  12  gg  ....... 
2    doctor question2?  13  hh  ....... 
2    doctor question2?  14  mm  ....... 

那麼你可以簡單的nodejs對數據進行排序 - 這樣你就只能做一個數據庫調用

你的方法,你會做一個請求到數據庫來獲取所有的問題,每個問題多一個請求得到它的答案 - 3請求總在你描述的情況。試想一下,你想拉的答案的50個問題 - 那麼你會做50個請求......


現在...讓我們對你的代碼的樣子。

首先,我看到嵌套for循環,即使用相同的變量名索引 - i決不做

// This is a completely WRONG code 
for (i = 0; i < question_ids.length; i++) { 
    ... 
    for (i = 0; i < results.length; i++) { 
     ... 
    } 
    ... 
} 

下一頁

不過,我只獲得了四個答案第一個問題, 不是四第二。

是的,這是因爲在getAnswers()方法你是解決您的第一個數據庫的響應承諾是真的。在這種情況下,你可以使Promise一個數組,並使用Promise.all()和代碼看起來就像這樣:

function getAnswers(question_ids) { 

    var promises = []; 

    for (var i = 0; i < question_ids.length; i++) { 
     promise = new Promise((resolve, reject) => { 
      connection.query(sql_command_2, question_ids[i], function (error, results) { 
       for (var j = 0; j < results.length; j++) { // change index with another letter here - j 
        answers.push(results[i].answer_text); 
        answer_ids.push(results[i].answer_id) 
       } 
       resolve(); 
      }) 
     }); 
     promises.push(promise); 
    } 

    return Promise.all(promises); 
} 

讓我知道,如果事情是不明確。祝你好運!

0

我找到了解決辦法,只是增加了一個if語句,以確保該問題數組的長度是成爲所需要的長度:

function getAnswers(question_ids){ 
    console.log(question_ids) 
    return new Promise((resolve, reject) => { 
    for (i = 0; i < question_ids.length; i++){ 
     connection.query(sql_command_2, question_ids[i], function(error, results){ 
     for (i = 0; i < results.length; i++){ 
     answers.push(results[i].answer_text); 
     answer_ids.push(results[i].answer_id) 
     } 
     if (answer_ids.length == (4 * question_ids.length)){ 
     console.log(answers) 
     console.log(answer_ids) 
     resolve({answers: answers, answer_ids: answer_ids}) 
    } 
    }) 
    } 
    } 
)} 
+0

@codtex是對的,他們的代碼更加高效:) –

+0

作爲一名程序員,您自己找到了解決方案,這是一個非常好的信號。我只是想把你的想法指向另一個更高效的方法,我很高興你明白它的工作原理:)幹得好! – codtex