如何将对象数组传递给jade模板?

and*_*a86 4 javascript jquery node.js express pug

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

这是对象

var objeto_img=
                            {
                                name:'name of the file',
                                image:'image.jpg url',
                                title:'title of the image',
                                caption:'descripcion of the image',
                                link:"#",
                            };
Run Code Online (Sandbox Code Playgroud)

在一些配置文件中有很多图像,所以这是一个像这样的对象数组

[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:"#",
                            };]
Run Code Online (Sandbox Code Playgroud)

这是服务器代码

res.render('index/perfil_publicacion_contenido',
    {
        datos:datosRecibidos
    })
Run Code Online (Sandbox Code Playgroud)

datosRecibidos是来自mongodb的一组对象

而我试图在玉里面放一个变量

input(type='hidden',id='imagenes',value=datos.img)
Run Code Online (Sandbox Code Playgroud)

但是当我试图获得对象时

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)
            }
Run Code Online (Sandbox Code Playgroud)

控制台日志打印未定义

这是为什么???如何在客户端正确地从db获取信息?tnx全部

Max*_*Max 9

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

- var jsonString = JSON.stringify(datos)
input(type='hidden',id='imagenes',value=jsonString)
Run Code Online (Sandbox Code Playgroud)

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

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

var fotos = $.parseJSON($('#imagenes1').val());
Run Code Online (Sandbox Code Playgroud)

我假设您使用$暗示您正在使用jQuery.

更新:解释

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

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

script
  var fotos = #{JSON.stringify(datos)};
Run Code Online (Sandbox Code Playgroud)

看看这个问题, 关于传递对象到页面上.