承诺-$ q.all后我得到相同的结果

阿里尔·温伯格(Ariel Weinberger)

我有以下代码:

return WordPress.getAllCategories()
  .then(function (cats) {
  var category = {};
  $q.all(cats.data.map(function (cat) {
    return WordPress.getLatestPostOfCategory(cat.id)
      .then(function (post) {
        return WordPress.getMediaById(post.data.featured_media)
          .then(function (media) {

            console.log('post id: ' + post.data.id);

            console.log('Post title: ' + post.data.title.rendered);

            category.post = {};
            category.post.id = post.data.id;
            category.post.title = post.data.title.rendered;
            category.post.content = post.data.content.rendered;

            var splitted = category.post.content.split('<p><!--more--></p>');
            category.post.introAsHtml = splitted[0];
            category.post.contentAsHtml = splitted[1];

            category.post.thumbnail = media.data.source_url;

            return category;
          });
      });
  })).then(function (res) {
    console.log(res);
  });
});

要为杂志主页加载每个类别的最新文章(将WordPress REST API与$ http请求一起使用)。过程如下:1.加载所有类别。2.获取每个类别的最新帖子。3.获取最新帖子的媒体。4.category.post基于发布数据构建对象,并从接收到的媒体(特定于发布)中添加缩略图。5.兑现所有诺言后,$scope.categories = categories提出申请。

问题:

使用上面的代码,我可以看到控制台日志正确地搜索了不同的帖子和媒体,但是最后我得到一个包含类别的数组,所有帖子都是相同的。相同的标题,内容,缩略图和所有内容。

我的诺言在这里做错了什么?

PS所有WordPresss服务功能均正常运行。在通过WordPress博客通过$ http请求接收必要的数据后,他们返回了已解决的承诺。

问候。

卡拉苏纳

尝试这种方式:

return WordPress.getAllCategories()
  .then(function (cats) {
  $q.all(cats.data.map(function (cat) {
    return WordPress.getLatestPostOfCategory(cat.id)
      .then(function (post) {
        return WordPress.getMediaById(post.data.featured_media)
          .then(function (media) {

            console.log('post id: ' + post.data.id);

            console.log('Post title: ' + post.data.title.rendered);

            var category = {}; // moved declaration here to return new instance each time
            category.post = {};
            category.post.id = post.data.id;
            category.post.title = post.data.title.rendered;
            category.post.content = post.data.content.rendered;

            var splitted = category.post.content.split('<p><!--more--></p>');
            category.post.introAsHtml = splitted[0];
            category.post.contentAsHtml = splitted[1];

            category.post.thumbnail = media.data.source_url;

            return category;
          });
      });
  })).then(function (res) {
    console.log(res);
  });
});

您正在返回相同类别的对象实例,每次在getMediaById回调中每次都创建一个新实例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以报告Q承诺进度而不创建延期吗?

来自分类Dev

AngularJS承诺$ q.all和SignalR

来自分类Dev

节点q承诺递归

来自分类Dev

$ q.all中的有角承诺不会被拒绝

来自分类Dev

$ q.all和嵌套承诺

来自分类Dev

$ q承诺无法解决

来自分类Dev

在承诺链中容纳Q.all

来自分类Dev

为什么我的Q承诺不起作用?

来自分类Dev

不要在顺序链接的承诺中包含q许诺结果

来自分类Dev

承诺:具有同步和异步功能的q.all()

来自分类Dev

q.all都不能满足多重承诺

来自分类Dev

Q承诺链错误后存在承诺链

来自分类Dev

Q承诺的同步答案

来自分类Dev

角$ q多重承诺

来自分类Dev

Q库(javascript)-使用Q.all()在承诺中处理q.reject()

来自分类Dev

使用Q轮询承诺

来自分类Dev

当某些承诺失败时,如何继续$ q.all()

来自分类Dev

承诺q.all结算为q.reduce

来自分类Dev

AngularJS承诺$ q.all和SignalR

来自分类Dev

节点q承诺递归

来自分类Dev

保持Q.all承诺序列汇总的干净方法

来自分类Dev

无法得到我的q承诺会捕获错误

来自分类Dev

AngularJS-$ q.all不以同步方式履行承诺

来自分类Dev

在承诺链中容纳Q.all

来自分类Dev

$ q服务承诺会返回相同的数据

来自分类Dev

Q承诺何时得到执行?

来自分类Dev

角$ q.all()不等待所有承诺

来自分类Dev

节点; Q承诺延迟

来自分类Dev

Angular:使用 $q.all 作为可选的承诺?