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

Javascript 如何将对象数组传递给jade模板?,javascript,jquery,node.js,express,pug,Javascript,Jquery,Node.js,Express,Pug,我想将一个对象数组从mongodb传递到客户端 这就是目标 var objeto_img= { name:'name of the file', image:'image.jpg url', title:'title of the image',

我想将一个对象数组从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的对象数组

我想在jade里面放一个变量

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 all

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

- var jsonString = JSON.stringify(datos)
input(type='hidden',id='imagenes',value=jsonString)
第一行应该将对象数组转换为字符串,然后可以将该字符串放入输入值中

然后,在读取值时,必须解析JSON

var fotos = $.parseJSON($('#imagenes1').val());
我假设您使用
$
意味着您正在使用jQuery

更新:说明

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

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

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

查看有关将对象传递到页面的信息。

您的问题不够清楚。你想完成什么?我想把对象数组放在输入隐藏的“imagenes”hoh中,所以我必须对数组进行字符串化并再次解析JSON以获得对象。。。谢谢你为我做这项工作!但是如果你能解释为什么我会这么感激,因为我不明白为什么我必须用parseJSON字符串化和还原这个过程。。。