Frage

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);
            };
    });
};
War es hilfreich?

Lösung

It looks like you might be new to Node.js, so you might want to read up on asynchronous programming with Node and the req and res objects used by Express. The two problems I see with your code right off the bat are:

  1. That you're assigning a value to your callback in BloggerService.js, rather than actually calling the callback: cb = client.blogger.posts.list(opts) should be (based on a quick scan of the Google API docs) client.blogger.posts.list(opts).execute(cb), and in case of an error cb = null should be cb(err).

  2. You're assigning a value to the response object, instead of sending a response: res = blogPosts should be res.json(blogPosts).

As far as where / when you call your service, the docs aren't intending to be cavalier. The services are globalized so that they can be called from anywhere within any controller; it's up to you as the developer to decide where you need your service calls to be!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top