I'm trying Sails JS and trying to display a simple record, but the records doesn't seem to be displaying. I have the code below, not sure what I'm doing wrong.
model:
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);
});
}
};
Controller:
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 });
},
View:
<html>
<body>
This is a test!
<%_.each(Model, function(myList){%>
<p>id: <%= myList.id%></p>
<% })%>
</body>
</html>
Your findAll
method is asynchronous, but you're treating it like a synchronous function by placing res.view
after it and expecting the myresult
var to be instantiated. Move res.view
inside the callback for listAll
(directly after console.log
) and it should work fine.