2015-12-23 72 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