在Sails JS中返回模型

时间:2014-08-11 23:00:41

标签: node.js sails.js

我正在尝试Sails JS并尝试显示一条简单的记录,但记录似乎没有显示。我有下面的代码,不知道我做错了什么。

模型:

module.exports = {

attributes: {
    id: {
        type: 'integer',
        autoIncrement: true
    },
    Name: {
        type: 'string'
    }
  },

  listAll: function(res, cb){
    Programmes.query('Select * from programmes', function(err, queryResult){
        cb(err, queryResult);
    });
  }
};

控制器:

  view: function (req, res) {

    var myresult;
    Programmes.listAll(res, function(err, result){
      var myresult = JSON.stringify(result);
      console.log('CONTROLLER:' + myresult)      
    });

    return res.view('Programmes/listAll', {Model : myresult });
  },

查看:

<html>
    <body>
        This is a test!
        <%_.each(Model, function(myList){%>
            <p>id: <%= myList.id%></p>
        <% })%>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您的findAll方法是异步的,但是您将它视为同步函数,方法是将res.view置于其后并期望实例化myresult var。在res.view的回调中移动listAll(直接在console.log之后),它应该可以正常工作。