简体   繁体   English

Javascript多个Promise无法与q一起使用并请求中间

[英]Javascript multiple promises not working with q and request middle

So this is my dilemma. 所以这是我的难题。 I have a list of movies, witch I have scraped from a website, then I want to add additional properties to my newly constructed object(json) 我有电影列表,我是从网站上抓取的,然后我想向我的新构造对象(json)添加其他属性

Now the omdi api witch I am using supports searching for a movie by title. 现在,我正在使用的Omdi api女巫支持按标题搜索电影。 Then I make a get request using request and q middlewares. 然后,我使用请求和q中间件发出一个获取请求。 When I receive information from omdb api in the call back I add that data to the object. 当我从回调中的omdb api接收信息时,我将该数据添加到对象中。

Now the next part is where my problem lies. 现在下一部分是我的问题所在。 Now I want to return a new Request using data from the previous request. 现在,我想使用先前请求中的数据返回一个新请求。 Now I make an new get Request and return it but then() func isin't returning anything. 现在,我发出一个新的get Request并返回它,但是then()func不返回任何东西。 But I don't seem to realize what I am doing wrong. 但是我似乎没有意识到我做错了什么。

Here is my code.. 这是我的代码。

var promises = [];
films.forEach(function (film) {
    // Get omdbapi information
    promises.push(HttpService.getContent(configExternal.omodburl + '?t=' + film.title.trim() + '&y=' + film.year + '&plot=true&tomatoes=true&r=json').then(function (data) { 
        var result = JSON.parse(data);
        if(Boolean(result.Response) === true) {
            film.omdb.push(result);
        }
        var imdbid = result.imdbID;
        return HttpService.getContent(configExternal.themoviedburl + imdbid + '/videos?api_key=' + configExternal.themoviedbkey);
    }).then(function(data) {
        film.trailers = [];
        film.trailers.push(JSON.parse(data));
    }).catch(function (err) {
        logger.error().info('Error getting ' + film.title + ' from omdb, ErrorMessage : ' + err);
    }));
});

//--------------------------------
// When all promises have finished
//--------------------------------
Promise.all(promises).then(function (data, err) {
    // do stuff with the data
});  

And here is my getContent func 这是我的getContent函数

var Service = {
    getContent: function(url) {
        var deferred = q.defer();
        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                deferred.resolve(body);
            } else {
                deferred.reject(error);
            }
        });
        return deferred.promise;
    }
};

Problem solved. 问题解决了。 There wasn't anything wrong with the request as Roamer said. 正如漫游者所说,请求没有任何问题。 But the moviedata base limits by 40 request per 10 sek witch I didn't know :) 但是moviedatabase每10 sek女巫限制40个请求,我不知道:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM