代码之家  ›  专栏  ›  技术社区  ›  Simon Hyll

Console.log未显示在节点的第二层中

  •  0
  • Simon Hyll  · 技术社区  · 7 年前

    我正在创建一个基于git提交的定制CHANGELOG生成器,使用 nodegit

    我现在的问题是,我似乎无法获得 console.log 在第二层中显示任何输出。

    此代码显示第一个 console.log 进入,但第二个消失在网络空间。它没有显示任何错误或任何东西,只是没有显示在控制台中。

    git.Repository.open(repo_root).then(function (repo_handle) {
      repo_handle.Tag.list(repo_handle).then(function (tag_list) {
        console.log("THIS SHOWS"); // <--- Shows
        repo_handle.getTagByName(tag_list[0]).then(function (tag) {
          console.log("THIS DOES NOT"); // <--- Doesn't show
        });
      });
    });
    

    只是为了验证问题不在 getTagByName 函数,下面的代码工作正常并输出 THIS SHOWS ,因此它与将日志功能放在承诺的第二层有关。

    git.Repository.open(repo_root).then(function (repo_handle) {
      repo_handle.getTagByName('v0.0.1').then(function (tag) {
        console.log("THIS SHOWS");
      });
    });
    

    顺便说一下,我已经尝试了同一代码的两个不同版本,例如使用 return repo_handle.Tag.list(repo_handle) 和 then(tag_list)

    据我所知,代码实际上没有任何错误或任何东西,代码似乎工作得很好,因为我没有收到任何错误,但是如果 console.log 如果工作不正常,那么也可能是因为它没有向我显示错误。。。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Simon Hyll    7 年前

    repo_handle.getTagByName('v0.0.1').then(function (tag) { 永远不会开始。试着这样做:

    git.Repository.open(repo_root).then(function (repo_handle) {
        repo_handle.Tag.list(repo_handle).then(function (tag_list) {
            console.log("THIS SHOWS"); // <--- Shows
            repo_handle.getTagByName(tag_list[0]).then(function (tag) {
                console.log("THIS DOES NOT"); // <--- Doesn't show
            }).catch(error => {
                console.log("gotcha! ", error);
            });
        });
    });
    

    来自asker的更新

    对于那些后来发现这一点的人来说,实际的问题是 repo_handle.Tag 是未定义的,由于添加了 catch 对于错误,因此接受这个答案。

    有效的新更新代码如下所示:

    let nodegit = require('nodegit');
    let path = require('path');
    
    var repo_root = path.resolve(__dirname, './.git');
    let repo = null;
    
    nodegit.Repository.open(repo_root)
      .then(function (repo_handle) {
        repo = repo_handle;
        return nodegit.Tag.list(repo_handle);
      })
      .then(function (tag_list) {
        return repo.getTagByName(tag_list[0]);
      })
      .then(function (tag) {
        console.log(tag.message());
      })
      .catch(function (e) {
        console.error(e);
      });
    
    推荐文章