So I've been tinkering with SailsJs and really like it so far, but I am trying to pull from posts from a blog I own into a view. This is a bit of a problem because the connection times out when trying to get the index view, and there is no feedback from the console via the console.log entries.
Blogger Service
// BloggerService.js - in api/services
var g = require('googleapis');
var apiKey = 'OUche33eekym0nKEY-uME';
exports.getBlogPosts = function(options, cb) {
g.discover('blogger', 'v3').execute(function(err, client) {
if(err) {
cb = null;
return console.log(err);
} else {
var opts = { 'blogId': options.id, 'maxResults': options.limit, 'fields': 'items(title,content,url)' };
cb = client.blogger.posts.list(opts);
};
});
};
exports.getBlogPost = function(options, cb) {
g.discover('blogger', 'v3').execute(function(err, client) {
if(err) {
cb = null;
return console.log(err);
} else {
var opts = { 'blogId': options.id, 'postId': options.postId };
cb = client.blogger.posts.get(opts);
};
});
};
Calling the service in the controller. Frustrating because the bottom of the documentation has a very cavalier way of saying where/how the service is called.
BlogController.js
/**
* BlogController.js
*
* @description ::
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
module.exports = {
index: function(req, res){
BloggerService.getBlogPosts({'id':'86753098675309','limit':6},function(err, blogPosts){
if(err){
return console.log(err);
} else {
console.log(blogPosts.items[0].url);
res = blogPosts;
};
});
}
}
Index view
<div>
<% _.each(Model.items, function (blogPost) { %>
<div class="panel panel-default">
<div class="panel-heading"><%= blogPost.title %></div>
<div class="panel-body"><%= blogPost.content %><input type="hidden" value="<%= blogPost.id %>"></div>
</div>
<% }) %>
</div>
Any help would be greatly appreciated. Thank you for the time you spent looking at this.
UPDATE
Many thanks to Scott, who got me closer to the end results. This is what I have thus far, but just need to clear up an authentication issue with discover/apiKeys.
exports.getBlogPosts = function(options, cb) {
g.discover('blogger', 'v3').execute(function(err, client) {
if(err) {
cb(err);
} else {
var opts = { 'blogId': options.id, 'maxResults': options.limit, 'fetchBodies': false, 'fields': 'items(title,url)' }
client.blogger.posts.list(opts).withApiKey(apiKey).execute(cb);
};
});
};
exports.getBlogPost = function(options, cb) {
g.discover('blogger', 'v3').execute(function(err, client) {
if(err) {
cb(err);
} else {
var opts = { 'blogId': options.id, 'postId': options.postId };
client.blogger.posts.get(opts).withApiKey(apiKey).execute(cb);
};
});
};