javascript - Can I render multiple sources in EJS - Stack Overflow

I am attempting to use data from 2 different sources, but render them on the same HTML page using EJS,

I am attempting to use data from 2 different sources, but render them on the same HTML page using EJS, JS and node. This is what I am trying..

app.set('view engine', 'ejs');
app.get('/', function(req, res) {
  res.render('index.ejs', { data: JSONdata })
  res.render('index.ejs', {data2: arrayData})
});

data is a JSON, data2 is an array. I have attempted to look up proper syntax for this exact process but cant seem to find anything.

Many thanks.

I am attempting to use data from 2 different sources, but render them on the same HTML page using EJS, JS and node. This is what I am trying..

app.set('view engine', 'ejs');
app.get('/', function(req, res) {
  res.render('index.ejs', { data: JSONdata })
  res.render('index.ejs', {data2: arrayData})
});

data is a JSON, data2 is an array. I have attempted to look up proper syntax for this exact process but cant seem to find anything.

Many thanks.

Share Improve this question edited May 17, 2016 at 22:40 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked May 17, 2016 at 22:39 user6067064user6067064
Add a ment  | 

2 Answers 2

Reset to default 4

You cannot render more than once to a single request.

But you could simply bine your JSON and array data and stringify it.

App.set('view engine', 'ejs');
app.get('/', function(req, res) {
  res.render('index.ejs', JSON.stringify({data2: arrayData, data1: JSONdata}))
});

Or simply assign both variables into a single object and parse it to the render function

var returnVals= JSON.stringify({data2: arrayData, data1: jsonData}); 

You cannot render more than once to a single request.

But if you want to show different types of data like:

SSCResult.find({username:username},function (err, results) {
  var username=req.user.username;
  var fullname =req.user.firstname+' '+req.user.lastname;
  if (err) return console.error(err);

  console.log(results);

  res.render('sscandhsc',{fullname:fullname,results});
  
});

SSCResult is a Schema. and results is like

[ { _id: 59f61fe2fec3cc7bf804f95e,
    examtype: 'HSC',
    username: '1',
    __v: 0,
    gpa: '5.00',
    institution: 'New Govt. Degree College, Rajshahi',
    passedyear: '2013',
    board: 'Rajshahi' },
  { _id: 59f6408efec3cc7bf804fc78,
    examtype: 'SSC',
    username: '1',
    __v: 0,

    gpa: '5.00',
    institution: 'Taragunia High School',
    passedyear: '2011',
    board: 'Jessore' },
  { _id: 59f656a9fec3cc7bf8050146,
    examtype: 'JSC',
    username: '1',
    __v: 0,
    gpa: '5.00',
    institution: 'Taragunia High School',
    passedyear: '2008',
    board: 'Jessore' } ]

so "results" and fullname is different types of json and you also can send it.

Lastly the upper(1) solution is also right form same type json file. Thank you. Hope it will help you. :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305635a4418881.html

相关推荐

  • javascript - Can I render multiple sources in EJS - Stack Overflow

    I am attempting to use data from 2 different sources, but render them on the same HTML page using EJS,

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信