2015-12-23 73 views
0

任何想法为什么从getBatchedContent返回的值为null,给定下面的代码示例?request-promise实现返回null`

import rp from 'request-promise'; 
import _ from 'lodash'; 

function getBatchedContent(size, page) { 
    return Promise.resolve(fetchBatchedContent(size, page)); // Getting `null` here. 
} 

function fetchBatchedContent(size, page) { 
    var options = { 
    uri: `http://www.example.com/posts?type=vogue-fashion-shows&filter[posts_per_page]=${size}&page=${page}` 
    }; 

    return rp(options) 
    .then(function (res) { 
     res = JSON.parse(res); 
     // res is an array of content objects. 
     return _.map(res, function (obj) { // logged value of `_.map()` is correct. 
     return transformContent(obj); 
     }); 
    }); 
} 

function transformContent(obj) { 
    return { 
    id: obj.ID, 
    title: obj.title, 
    // etc. 
    }; 
} 
+0

仅供参考,'getBatchedContent()'不提供其他功能。你可以直接调用'fetchBatchedContent()'并获得相同的结果。 – jfriend00

+0

如果你正在谈论解决的承诺价值为'null',那么我会看你的'rp(...)。then()'处理,看它是否真的在做你期望的并且实际返回期望值。 – jfriend00

回答

1

也许增加一个.catch(function (error) {debugger;})fetchBatchedContent。它是否会返回您所期望的?

我不确定您输入的Promise.resolve可以直接导致null?这对我来说似乎很奇怪,即使Promise.resolve(Promise.reject('foo'))也会返回一个Promise。 (被拒绝的)。

+0

嗯......我应该澄清一下,'null'值是由hapi返回的json响应所起的作用。我试图将实施的这一部分置之不理,以找出问题的原因。然而......在'fetchBatchedContent'主体的'return'语句中的'then'后添加'catch'不会导致任何捕获错误。 – jerome

+0

那么'getBatchedContent'的承诺返回,解析为'null'? – Norbert