我正在尝试学习 Node.js,我现在有一个非常好的应用程序,但我想实现一个库(venom-bot)来发送一些短信。我的问题是我在尝试使用主文件之外的函数时遇到了麻烦。这是我的代码:
// Dependencies
const server = require("./lib/server")
const venom = require("venom-bot")
// Declare the app
const app = {}
// Init function
app.init = () => {
// Start the server
server.init()
}
// Init venom-bot
venom.create().then((client) => start(client))
// A function to test if I can send the message
async function testing(msg) {
await msg.sendText(...some code here...)
}
function start(client) {
app.msg = client
// Here, if I pass app.msg as an argument, works
// My problem is that I can't use app.msg outside of here,
// even with the module.exports down there
// (I'm trying to use it on a helpers.js file).
testing(app.msg)
// Execute the application
app.init()
}
// Export the app
module.exports = app
在helpers.js文件中,我需要这样:
// Dependencies
const app = require("./index.js")
// A function to test
async function sendMsg(msg) {
await msg.sendText(...some code here...)
}
helpers.send = () => {
sendMsg(app.msg)
}
module.exports = helpers
每当helpers.send
被调用时,它应该正确使用helpers.send
传递app.msg
as 参数上方的 async 函数,至少我认为应该如此。我在这里缺少什么?
我得到的错误是:
(节点:18148)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“sendText”
出于测试目的,当我收到对指定路线的获取请求时,我试图简单地调用该助手。