NodeJS:从同一文件中的另一个函数内部调用函数

NodeJS: call func from inside another func in same file

我有 NodeJS 程序。

在一个 class 中,我有各种实用方法。一个函数 safeGithubPush 调用 safeString,另一个函数在同一个 class

module.exports = {

  safeString(stringToCheck) {
    console.log(validator.isAscii(stringToCheck), validator.matches(stringToCheck, /^((\w)*[-.]?(\w)*)*$/))
    return (
      validator.isAscii(stringToCheck) &&
      validator.matches(stringToCheck, /^((\w)*[-.]?(\w)*)*$/)
    );
  },

  safeGithubPush(currentJob) {
    if (
      !currentJob ||
      !currentJob.payload ||
      !currentJob.payload.repoName ||
      !currentJob.payload.repoOwner ||
      !currentJob.payload.branchName
    ) {
      this.logIn(
        currentJob,
        `${'    (sanitize)'.padEnd(15)}failed due to insufficient job definition`
      );
      throw invalidJobDef;
    }

    if (
      this.safeString(currentJob.payload.repoName) &&
      this.safeString(currentJob.payload.repoOwner) &&
      this.safeString(currentJob.payload.branchName)
    ) {
      return true;
    }
    throw invalidJobDef;
  },

} 

虽然实用程序 class 中的另一个函数 this.logIn() 工作正常,但我收到 safeString 的错误:

Error caught by first catch: TypeError: this.safeString is not a function

我关注了a solution offer by another SO post:

safeString: function(stringToCheck){
...
}

safeGithubPush(currentJob) {
...
if (
    this.safeString(currentJob.payload.repoName) &&
    this.safeString(currentJob.payload.repoOwner) &&
    this.safeString(currentJob.payload.branchName)
    ) {
       return true;
   }
}

但是这样也得到一个,TypeError: this.safeString is not a function

我没有使用箭头函数,它是

我认为您当前提供的代码无法确定原因。这可能与您的通话方式有关 safeGithubPush。如果你做了一些会改变 this 绑定的事情,this.safeString 将会失败。

const foo = {
  fizz() {
    console.log("fizz");
  },
  buzz() {
    this.fizz();
  }
};

// "this" is correct
foo.buzz();

// "this" has no fizz to call
const myFizz = foo.buzz;
myFizz();

考虑到您将这些附加到 module.exports 我猜想您在 require 调用中关闭了这些功能,然后尝试裸露使用它们,这使得问题在查看后很明显我上面的例子:

// Ignore these 2 lines, they let this look like node
const module = {};
const require = () => module.exports;
// Ignore above 2 lines, they let this look like node

// Your module "some.js"
module.exports = {
  safeString(str) {
    return true;
  },
  safeGithubPush(currentJob) {
    if (!this.safeString("some")) {
      throw new Error("Not safe");
    }
    return true;
  }
};

try {
  // Some consumer module that doesn't work
  const {safeGithubPush} = require("./some.js");

  const isItSafe = safeGithubPush();

  console.log(`Safe? ${isItSafe}`);
} catch (err) {
  console.error("Didn't bind right \"this\"");
}

try {
  // Some consumer module that DOES work
  const someModule = require("./some.js");

  const isItSafe = someModule.safeGithubPush();

  console.log(`Safe? ${isItSafe}`);
} catch (err) {
  console.error(err);
}

我会重构这段代码。你说这些是实用函数,这让我觉得你真的不想在考虑 this 的情况下构建它们。

与其在定义时将它们全部附加到 module.exports,不如在外部定义它们并直接引用您要使用的函数,然后 将它们附加到 exports 以便其他模块可以使用这些功能:

function safeString(stringToCheck) {
  return true;
}

function safeGithubPush(currentJob) {
  if (!safeString("some")) {
    throw new Error("Not safe");
  }
  return true;
}

module.exports = {
  safeString,
  safeGithubPush
};