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?