如何使用 Sails 从控制器传递到视图

How to pass from controller to view using Sails

我阅读了 sails 文档,显然我做的是正确的,但我一直收到 undefined.
我的模型:

module.exports = {
schema:true,
conection:'NodeSailsMsSql',
tableName:'contactos',
autoUpdatedAt: false,
autoCreatedAt:false,

attributes: {

nombre:{
  type:'string',
  required:true
},
telefonocel:{
  type:'int'
},
telefonocasa:{
  type:'int'
},
telefonotrabajo:{
  type:'int'
},
correo:{
  type:'string',
  email:true
},
idtipocontacto:{
  type:'integer',
  required:true
}

}
};

我的控制器

agregar: function(req,res,next){
Contactos.create(req.allParams(),function contactosagregados(err, contactos) 
{
  if(err)return next(err);
  res.redirect('contactos/mostrarcontactos/'+contactos.id);
});
},
mostrarcontactos: function(req,res,next){
Contactos.query('select * from contactos where id='+('id'),function 
contactosencotrados(err, contactos) {
  if(err)return next(err);
  if(!contactos) return next();
  res.view({
    contacto:contactos
  });
});
},

查看

<h1><%= contacto.id %></h1>
<hr>
<h3>Correo:<%= contacto.Correo %></h3>
<h3>Nombre: <%= contacto.Nombre %></h3>

如果我调试数据在 contacto 上的控制器上,但不知何故数据在传递给视图时丢失了。

在您的查询中,您似乎要查询 ID 字面上为 'id' 的记录。我想你一定是想以某种方式从请求中获取 id?可能是这样的:

mostrarcontactos: function(req,res,next){
  var inputId = req.param('id');
  Contactos.query('select * from contactos where id=' + inputId, function contactosencotrados(err, contactos) {
    // you should check what your result looks like. With .query that depends on which database you are using
    sails.log(contactos);
    if(err) {return next(err);}
    if(!contactos) {return next();}
    res.view({
      contacto:contactos
    });
  });

},

试一试,确保您尝试在视图中访问的属性与检索到的数据相匹配 contactos