簡體   English   中英

Sails.js模型,從mysql數據庫中選擇特定的列

[英]Sails.js model, select a specific column from mysql database

我在模板文件中未定義GetName遇到問題。

代碼: http : //hastebin.com/wixomerapi.js

但是如果我寫

console.log(results[0].name);

代替GetName = results[0].name正常工作!

那這怎么了? :/

更好的解決方案是使用options參數並設置select。

Model.find({field: 'value'}, {select: ['id', 'name']})
  .paginate({page: 1}, {limit: 10)
  .exec(function(err, results) {
    if(err) {
      res.badRequest('reason');
    }
    res.json(results);
});

嘗試這個,

ViewGame: function (req, res) {
    var GetName;
    Games.findBySlug(req.params.tournamentName).exec(function(err, results) { 
    GetName = results[0].name;
    res.view('game/view', {
        name : GetName,
    });
 });        
},

您必須學習Javascript回調的工作方式。

在您的代碼函數中,在調用ViewGame函數后,將按以下順序執行:

  1. 調用Games.findBySlug(req.params.tournamentName).exec()函數可調度以下回調函數,以供稍后調用。

     function(err, results) { GetName = results[0].name; }); 
  2. 調用以下函數。

     res.view('game/view', { name : GetName, }); 
  3. 調用以下功能(在第一步驟預定)。

     function(err, results) { GetName = results[0].name; }); 

由於在第二步的“ res.view”中設置了此GetName,因此未分配該GetName(在第三步進行分配)。 但是,當您使用“ console.log”時,它僅在第三步中打印“ results [0] .name”的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM