Thursday 11 January 2018

Render array on a single page with nodejs

I'm developing a little web application that post tweets. One thing I want to do is display information about the tweets posted, like retweets,like etc. I'm using nodejs + mongodb. I have an array pulled from mongodb with the IDs of the tweets, then I use every single ID to query the twitter API and then try to render the results. The problem is that I can't render the page multiple times, so it gives me an error. Here's some code of the structure:This is in the index.ejs, to print the results in the div result:$.ajax({ type: 'GET', url: '/pullData', success: function(result) { $('#results').html(result); } This is in the app.js , the main nodejs app:app.get("/pullData",function(req,res){ mongo.pullData(twitter,res); }); Function pulldata in mongodb.ejs:exports.pullData = function(twitter,res){ MongoClient.connect(url, function(err, db) { if (err) throw err; db.collection("tweets").find({}).toArray(function(err, result) { result.forEach(function(entry) { twitter.loadTwit(res,entry.id); }); db.close(); }); }); } The twitter function to interrogate the API:exports.loadTwit = function(res,value){ T.get('statuses/show/'+value, function (err, data, response) { res.render('db',{data: data}); }); } The db.ejs page to be writed in the previous result div:ID: <%=data.id%> Retweet Count: <%=data.retweet_count%> Favorite Count: <%=data.favorite_count%>

Submitted January 11, 2018 at 07:20PM by snakethesniper

No comments:

Post a Comment