简体   繁体   English

将嵌套数组从node.js服务器传递到ejs页面

[英]Passing a nested array from node.js server to ejs page

I have this object: 我有这个对象:

 { id: 22, user: 'Username', born: '06/10/1975', ralationships: [ { profession: 'Joiner' }, { profession: 'Farmer' }, { company: 'Summer Sun Ranch', nickname: 'SunRanch' } ] } 

The above array is the output of a console.log(myArr[x]); 上面的数组是console.log(myArr[x]);的输出console.log(myArr[x]);

Then I pass it to ejs trough 然后我将其传递给ejs槽

  res.render('index', {
            lista: myArr
        });

The ejs index is this: ejs索引是这样的:

 <h1>Persone</h1> <ul> <% lista.forEach(function(elemento,i){ %> <li> <%= elemento.user %> </li> <ul> <% for (var j = 0; j < elemento[i].relationships.length; j++) {%> <li> <%= elemento[i].relationships[j] %> </li> <% } %> </ul> <% }) %> </ul> 

I am completely unable to handle the relationship sub-array in any way (the above code is just one of many try). 我完全无法以任何方式处理关系子数组(上面的代码只是许多尝试中的一种)。 The sub array seems missing. 子数组似乎丢失。

Update: in the ejs I put this line 更新:在ejs中,我把这一行

<%- JSON.stringify(elemento) %>

I receive back this: {"id":22,"user":"Username","born":"06/10/1975","relationships":[]} 我收到以下信息: {"id":22,"user":"Username","born":"06/10/1975","relationships":[]}

This means that the relationships are passed empty from the node.js server What's wrong? 这意味着关系从node.js服务器传递为空,这是怎么回事? Thanks a lot! 非常感谢!

 lista.forEach(function(elemento,i){ 

So elemento is the object which has a user property and a ralationships property. 所以elemento是具有目标user属性和ralationships财产。

And i is the position that object olds in the lista array. i是对象在lista数组中变lista的位置。

Thus: 从而:

 elemento[i].relationships 

… is nonsense: elemento is not lista . …是胡说八道: elemento不是lista

You could use 你可以用

elemento.relationships

or 要么

lista[i].relationships

(You also need to spell ralationships (sic) consistently). (您还需要拼ralationships (原文如此)一致)。

The problem was in the server.js. 问题出在server.js中。 Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM