好吧,我有路線:出口中的NodeJS
var express = require('express');
var router = express.Router();
var articles = require('../model/articles.js');
router.get('/all', function(req, res, next) {
res.json(articles.getAll());
console.log("From route: " + articles.getAll());
});
module.exports = router;
和,我有模式:
var mysql = require("mysql");
var con = mysql.createConnection({
host: "localhost",
user: "rest_news",
password: "rest_news",
database: "rest_news"
});
exports.getAll = function() {
con.query('SELECT * FROM articles', function(err, rows){
if(err) {
return err;
} else {
console.log("From model: " + rows);
return rows;
}
});
};
我需要從MySQL獲得的所有文章,即時使用該模型文章,和方法getAll()。我在控制檯中看到:
From route: undefined
GET /article/all 200 22.802 ms - -
From model: [object Object],[object Object]
From model: [object Object],[object Object]
getAll()在第二次工作,爲什麼?
無法正常工作,因爲您正在處理異步代碼,如同步代碼。 getAll函數不返回任何東西。 – migg