如何将一个对象数组传递给玉模板? [英] how to pass an array of objects to jade template?

查看:109
本文介绍了如何将一个对象数组传递给玉模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to pass an array of objects from mongodb to the client...

这是对象

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

所以这是一个像这样的对象数组

in some profiles the are many images so the is an array of objects like this

[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的对象数组

datosRecibidos is an array of objects from mongodb

,并尝试在玉中放入一个变量

and im trying to put in a variable inside 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全部

why is that??? how can i get the information from db correctly in the client??? tnx all

推荐答案

如果我理解正确,你想将对象数组序列化到的输入。尝试这样:

If I understand correctly, you want to serialize the array of objects into the value of the input. Try this:

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

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

The first line should turn the array of objects into a string which can then be placed into the value of the input.

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

Then, when you read the value, you will have to parse the JSON.

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

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

I'm assuming that your use of $ implies that you are using jQuery.

更新:说明

如果你想要一个在您的服务器内存中的对象可以在浏览器中运行的Javascript中使用,您必须将该对象写入到该页面上。这个过程被正式称为序列化,使用Javascript的方法是 JSON.stringify 。在输入的的页面上,它只是一个String。页面上的Javascript必须将该String转换为对象(或在本例中为对象数组)。这就是JSON.parse所在的地方,因为较老的浏览器没有JSON.parse,所以应该使用像jQuery.parseJSON这样的polyfill来确保更老的浏览器能够将String转换成Javascript对象。

If you want an Object that is in memory in your server to be available to the Javascript running in the browser, you have to "write" that Object onto the page. That process is officially called serialization and the way to do that with Javascript is JSON.stringify. Once on the page in the value of the input, it is just a String. Javascript on the page has to turn that String into an Object (or in this case, an Array of Objects). That's where JSON.parse comes in. Because older browsers don't have JSON.parse, you should use a polyfill like jQuery.parseJSON to ensure that even older browsers will be able to turn the String into a Javascript Object.

顺便说一下,如果您不需要在隐藏的数据 输入但你认为这是最好的方法,让我建议另一种方式。如果您的目标是从服务器中 var fotos 包含 datos 的值,则可以直接在< script> 标签。以下是在Jade中执行的操作:

By the way, if you don't need the data specifically in the hidden input but you think that that is the best way to do it, let me suggest another way. If your goal is to have var fotos contain the value of datos from the server, you can do that directly in a <script> tag on the page. Here's how to do it in Jade:

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

查看

Check out this question about passing Objects to the page.

这篇关于如何将一个对象数组传递给玉模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆