【问题标题】:FIXED: SailsJs + Google Blogger is timing out已修复:SailsJs + Google Blogger 超时
【发布时间】:2014-04-28 01:45:59
【问题描述】:

所以我一直在修补 SailsJs 并且到目前为止非常喜欢它,但我正试图从我拥有的博客中提取帖子到视图中。这有点问题,因为在尝试获取索引视图时连接超时,并且没有来自控制台通过 console.log 条目的反馈。

博客服务

// 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);
            };
    });
};

在控制器中调用服务。 Frustrating because the bottom of the documentation has a very cavalier way of saying where/how the service is called.

博客控制器.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;
                    };
            });
    }
}

索引视图

<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>

任何帮助将不胜感激。感谢您花时间查看此内容。

更新

非常感谢 Scott,他让我更接近最终结果。到目前为止,这就是我所拥有的,但只需要使用 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);
            };
    });
};

【问题讨论】:

    标签: google-api blogger sails.js


    【解决方案1】:

    看起来您可能是 Node.js 的新手,因此您可能需要阅读 asynchronous programming with Node 和 Express 使用的 req and res objects。我立即看到您的代码存在的两个问题是:

    1. 您正在为BloggerService.js 中的回调分配一个值,而不是实际调用回调:cb = client.blogger.posts.list(opts) 应该是(基于对 Google API 的快速扫描docs)client.blogger.posts.list(opts).execute(cb),如果出现错误,cb = null 应该是cb(err)

    2. 您正在为响应对象分配一个值,而不是发送响应:res = blogPosts 应该是 res.json(blogPosts)

    就您在哪里/何时调用您的服务而言,这些文档并不打算傲慢。服务是全球化的,因此可以从任何控制器内的任何位置调用它们;由您作为开发人员来决定您需要将服务调用放在哪里!

    【讨论】:

    • 准确地回答了我需要知道的内容。即使进行了粗略的检查,您也成功了。非常感谢。我只是在设置 ApiKey 时遇到问题,但这超出了我的问题范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多