admin管理员组文章数量:1289845
i want to pass an array of objects from mongodb to the client...
this is the object
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:"#",
};]
this is the server code
res.render('index/perfil_publicacion_contenido',
{
datos:datosRecibidos
})
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)
but when i try to get the objects
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)
}
the console log print undefined
why is that??? how can i get the information from db correctly in the client??? tnx all
i want to pass an array of objects from mongodb to the client...
this is the object
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:"#",
};]
this is the server code
res.render('index/perfil_publicacion_contenido',
{
datos:datosRecibidos
})
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)
but when i try to get the objects
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)
}
the console log print undefined
why is that??? how can i get the information from db correctly in the client??? tnx all
Share Improve this question asked Oct 18, 2012 at 17:44 andrescabana86andrescabana86 1,7888 gold badges31 silver badges56 bronze badges 2- 2 Your question is not clear enough. What are you trying to acplish? – Max Commented Oct 18, 2012 at 18:22
- i want to put the array of objects inside the input hidden "imagenes" – andrescabana86 Commented Oct 18, 2012 at 18:41
1 Answer
Reset to default 9If 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.
Then, when you read the value, you will have to parse the JSON.
var fotos = $.parseJSON($('#imagenes1').val());
I'm assuming that your use of $
implies that you are using jQuery.
UPDATE: Explanation
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 es 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.
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.
本文标签: javascripthow to pass an array of objects to jade templateStack Overflow
版权声明:本文标题:javascript - how to pass an array of objects to jade template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741448998a2379376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论