1
節點JS快遞試圖一個ORM JSON傳遞到客戶端在我app.js
question = require('./routes/question_api'),
app.use(orm.express("mysql://[email protected]/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返回,它會起作用。
您好,感謝您的快速反應。我得到'ReferenceError:回調未定義' – WebQube
您是否將參數'res'重命名爲'callback'?我編輯我的帖子,使其更清晰。 – Sonata
謝謝,那工作。對於noob問題感到抱歉,對節點j而言是新的。 – WebQube