Node console.log / res.render Two Different Data Sources / Types

Viewed 27

Ultimately, I'm trying to get two different calls to data to res.render on a page. In the process, I console.log to see if the data could eventually be sent to the page from res.render. 1 out of the 5 console.logs does not work, #4, and I don't understand why.

Here's what I have... server.js code

const uri = 'neo4j address';
const user = 'uname';
const password = 'pword'
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const session = driver.session({ database: 'dbname' });

app.get('/page', async (req, res) => {
  try {
    const dbTitleResult = await session.run(`MATCH (db:Database)
              RETURN db.wikiPageId as dbWikiPageId, db.Title as dbTitle Order By db.Title ASC`); 
    const dbTitleArr = dbTitleResult.records.map(({_fields}) => {
    return {dbWikiPageId:_fields[0],dbTitle:_fields[1]};
});

    const wikiPage = request('https://uname:pword!@wiki.com/api/content/id/child?expand=page',  function (error, response, body) {
  
      if (error) throw new Error(error);
      const wikiAPI = JSON.parse(body);
      //console.log(wikiAPI.page.results); //#1 THIS WORKS AND RETURNS THE EXPECTED DATA

      const wikiData = wikiAPI.page.results.map(item => item);        
      //console.log(wikiData);  //#2 THIS WORKS AND RETURNS THE EXPECTED DATA
}); 

//console.log(dbTitleArr);  //#3 THIS WORKS AND RETURNS THE EXPECTED DATA
//console.log(wikiData);    //#4 THIS DOES NOT WORK

    res.render('page.ejs', {dbTitle: dbTitleArr});
    //console.log(dbTitleArr);  //#5 THIS WORKS AND RETURNS THE EXPECTED DATA
  } catch(e) {
    console.log("Something went wrong", e) }
});

This is what seems inconsistent and what I appear to need help with, console.log #3 is successful but console.log #4 is not. console.log #4 gives the error of Something went wrong ReferenceError: wikiData is not defined. But it would seem to be defined in the same way that dbTitleArr is defined? How do I make #4 console.log-able? Or why is it not console.log-able like #3?

For actually using the returned data, I can res.render the dbTitleArr data (and I can view it on page.ejs), I cannot res.render the wikiData data. What do I need to do to see #4 console.log wikiData and ultimately res.render it?

1 Answers

I was able to ultimately res.render both sets of data, but #4 console.log still does not work. I realize that was the initial question as to why it did not work, so I'll provide someone else with the answer points if they can explain why #3 works but #4 doesn't. ...

What I needed to do was move my res.render into the request code (and I also removed the const wikiPage in front of the request).

app.get('/page', async (req, res) => {
  try {
   
    const dbTitleResult = await session.run(`MATCH (db:Database)
    RETURN db.wikiPageId as dbWikiPageId, db.Title as dbTitle Order By db.Title ASC`); 
const dbTitleArr = dbTitleResult.records.map(({_fields}) => {
return {dbWikiPageId:_fields[0],dbTitle:_fields[1]};
});

    request('https://uname:pword!@wiki.com/api/content/id/child?expand=page',  function (error, response, body) {
  
      if (error) throw new Error(error);
      const confluenceAPI = JSON.parse(body);
      //console.log(confluenceAPI.page.results); //#1 WORKS

      const wikiData = confluenceAPI.page.results.map(item => item);        
   
      //console.log(wikiData);  //#2 WORKS
      res.render('page.ejs', {dbTitle: dbTitleArr, wikiData});
}); 
//console.log(dbTitleArr);  //#3 WORKS
//console.log(wikiData);    //#4 STILL DOES NOT WORK, BUT OK SINCE IT RENDERS

  } catch(e) {
    console.log("Something went wrong", e)
  }
});
Related