【问题标题】:how to pass an array of objects to jade template?如何将对象数组传递给玉模板?
【发布时间】:2012-10-09 06:15:40
【问题描述】:

我想将一组对象从 mongodb 传递给客户端...

这是对象

var objeto_img=
                            {
                                name:'name of the file',
                                image:'image.jpg url',
                                title:'title of the image',
                                caption:'descripcion of the image',
                                link:"#",
                            };

在某些配置文件中,图像很多,因此是这样的对象数组

[var objeto_img=
                            {
                                name:'name of the file',
                                image:'image.jpg url',
                                title:'title of the image',
                                caption:'descripcion of the image',
                                link:"#",
                            },var objeto_img=
                            {
                                name:'name of the file',
                                image:'image.jpg url',
                                title:'title of the image',
                                caption:'descripcion of the image',
                                link:"#",
                            },var objeto_img=
                            {
                                name:'name of the file',
                                image:'image.jpg url',
                                title:'title of the image',
                                caption:'descripcion of the image',
                                link:"#",
                            };]

这是服务器代码

res.render('index/perfil_publicacion_contenido',
    {
        datos:datosRecibidos
    })

datosRecibidos 是一个来自 mongodb 的对象数组

我试图在玉中放入一个变量

input(type='hidden',id='imagenes',value=datos.img)

但是当我尝试获取对象时

var fotos=$('#imagenes1').val();
            for(foto in fotos)
            {
                console.log(fotos[foto].image)
                console.log(fotos[foto].name)
                console.log(fotos[foto].caption)
                console.log(fotos[foto].title)
            }

控制台日志打印未定义

这是为什么???我怎样才能在客户端正确地从 db 获取信息??? tnx 全部

【问题讨论】:

  • 你的问题不够清楚。你想完成什么?
  • 我想将对象数组放入输入隐藏的“imagenes”中

标签: javascript jquery node.js express pug


【解决方案1】:

如果我理解正确,您希望将对象数组序列化为输入的value。试试这个:

- var jsonString = JSON.stringify(datos)
input(type='hidden',id='imagenes',value=jsonString)

第一行应该将对象数组转换为字符串,然后可以将其放入输入的值中。

然后,当您读取该值时,您将不得不解析 JSON。

var fotos = $.parseJSON($('#imagenes1').val());

我假设您使用 $ 意味着您正在使用 jQuery。

更新:解释

如果您希望服务器内存中的对象可供浏览器中运行的 Javascript 使用,则必须将该对象“写入”到页面上。该过程正式称为序列化,而使用 Javascript 的方法是 JSON.stringify。一旦在输入的value 页面上,它只是一个字符串。页面上的 Javascript 必须将该字符串转换为对象(或在本例中为对象数组)。这就是 JSON.parse 的用武之地。因为旧版浏览器没有 JSON.parse,您应该使用像 jQuery.parseJSON 这样的 polyfill 来确保即使是旧版浏览器也能够将 String 转换为 Javascript 对象。

顺便说一句,如果您不需要hidden input 中的数据,但您认为这是最好的方法,让我建议另一种方法。如果您的目标是让var fotos 包含来自服务器的datos 的值,您可以直接在页面上的<script> 标记中执行此操作。以下是在 Jade 中的操作方法:

script
  var fotos = #{JSON.stringify(datos)};

查看this question关于将对象传递到页面的信息。

【讨论】:

  • hoh 所以我必须对数组进行字符串化并再次 parseJSON 以获取对象...谢谢您为我所做的工作!但是如果你能解释为什么我会理解,因为我不明白为什么我必须用 parseJSON 字符串化和恢复过程......
猜你喜欢
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 2015-08-24
  • 1970-01-01
  • 2019-03-11
  • 2017-08-04
  • 2016-03-14
相关资源
最近更新 更多