如何获取正在调用函数的文件



我的主应用程序文件中有这个全局函数

async getTip(){
const tipschema = require('./schemas/tipschema')
const tipschemas = await tipschema.find()
const randomschema = tipschemas[Math.floor(Math.random()*tipschemas.length)]
const randomtip = randomschema.tip
return randomtip
},

有什么方法可以得到调用函数的文件名吗?例如:

async getTip(){
const file = await req.file // example of what I'm trying to do
},

感谢

你可以做:

function getTip() {
let caller = getTip.caller;
}

但这很糟糕。也可以看这里:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

还要注意,这不适用于严格模式。

最新更新