2017-09-25 9 views
0

私は自分のノードmysql設定を動作させようとしています。私のモデルevent.jsでは、私は次のコードを持っている:モデルからの結果をインデックスファイルに渡すことができません

Event.getEvents = function(results) { 
    db.query("select * from com_event_details where type='featured'", function(error, results, fields) { 
    return results; 
    }); 
}; 

をそして、私のindex.jsに私はgetEventsから結果を返すようにしてみてください。

app.get('/', function(req, res) { 
    Event.getEvents(function(results) { 
    res.send(results); 
    }); 

});

残念ながら、これは機能しません。どういうわけか、モデルからの結果をインデックスファイルに渡すことができません。

あなたのお手伝いをお待ちしております。

答えて

1

コールバックをEvent.getEventsに渡していますが、その関数ではコールバックを使用しませんでした。

使用この代わりに:

Event.getEvents = function(callback) { 
    db.query("select * from com_event_details where type='featured'", callback); 
}; 


app.get('/', function(req, res) { 
    Event.getEvents(function(error, results) { 
    res.send(results); 
    }); 
}); 
0

function getResult() { 
    return new Promise(function(resolve, reject) { 
     try { 
      db.query("select * from com_event_details where type='featured'", function(error, results, fields) { 
       resolve(results); 
      }); 
     } catch (ex) { 
      reject(ex); 
     }; 
    }); 
} 
以下のようにあなたが結果を返すためにノードでの約束を使用することができます

関連する問題