I am developing simple web app that asks for Facebook access token and retrieve 'post' data of logged-in user.
After clicking 'login', it calls FB.api to retrieve user posts and use innerHTML in javascript to update user-status:
<div id="fb-root"></div>
<div id="user-info"></div>
<div id="user-email"></div>
<div id="user-status" name="post"></div>
<button id="fb-auth">Login</button>
My goal is to persist these updated data into mongodb.
What i have tried is to force to submit the form when the post is retrieved and pass the data in 'name="post"', However, this makes the page refreshes again and also the value shows undefined.
Javascript:
FB.api('/me/posts', { limit: 100 }, function(response) {
var userStatus = document.getElementById('user-status');
for (var i=0, l=response.data.length; i<l; i++) {
var post = response.data[i];
if (post.message) {
userStatus.innerHTML = userStatus.innerHTML + ' ' + post.message ;
var form = document.createElement('form');
form.setAttribute('method', 'post');
form.style.display = 'hidden';
document.body.appendChild(form);
form.submit();
} else if (post.attachment && post.attachment.name) {
alert('Attachment: ' + post.attachment.name);
}
}
});
app.js(req.param('post') is undefined, thus mongodb injects undefined data):
app.post('/', function(req, res){
postProvider.save({
posts: req.param('post')
}, function(error, docs){
res.redirect('/')
});
});
Any advice will be greatly appreciated. Thank you for your time.
To get form data you have to use:
req.body.post