在我的 app.js
question = require('./routes/question_api'),
app.use(orm.express("mysql://root@127.0.0.1/daat", {
define: function (db, models, next) {
var question_model = db.define('questions', {
id : Number,
..... more definitions
}
});
models.question = question_model;
next();
}
}));
在我的 question_api.js
var questions = require('../lib/questions/questions.js');
//NEW Question
//Get Question by ID
//Get Question By Category
exports.list = function(req, res){
res.json(questions.list_top_rates(req, res));
};
在我的 questions.js 中
exports.list_top_rates = function(req, res) {
var limit = req.limit || 20;
var final_result = {"1": {}};
//Z means descending
req.models.question.find({}, limit, ["rating", "Z"], function(err, questions) {
final_result = questions;
return final_result[0];
});
return final_result;
// return final_result;
// return {
// 1: "first question , limit is " + limit,
// 2: "second question",
// 3: "third question",
// 4: "forth question"
// }
};
我得到空的结果。如果我直接从 question_api.js 返回,它可以工作。