gpt4 book ai didi

node.js - 导出/导入Nightmare.js函数

转载 作者:行者123 更新时间:2023-12-03 13:23:47 24 4
gpt4 key购买 nike

因此,我有一个工作正常的nightmare.js应用程序,该应用程序可以100%工作。我现在处于重构阶段,想将我制作的自定义函数(使用nightmare.js函数)放入另一个文件,然后将其导出/导入到我的主文件中。

这些函数会被调用,但 Nightmare 函数实际上不会执行或引发错误。

为什么导入梦the功能时不起作用?

我的主要应用程序:

const Nightmare = require('nightmare')
const nightmare = Nightmare({
show: true,
typeInterval: 1000,
waitTimeout: 60 * 1000
})

const bot = require('./utils')

nightmare
.goto(url)
.then(_ => bot.selectByVal('#myDiv', 'myVal'))
.then( 'yada yada yada ...')...

module.exports = nightmare;

实用程序:
const Nightmare = require('nightmare');
const nightmare = Nightmare();

module.exports = {
selectByVal: function(el, val) {
console.log('select' + el + val)
try {
return nightmare.select(el, val)
} catch (e) {
return e
}
}
}

我认为这与我的 Nightmare 实例没有导出/导入有关,但不确定如何执行此操作。

最佳答案

botutils无权访问在主应用程序上创建的nightmare。您需要通过引用。

返回一个返回对象的函数。

module.exports = function(nightmare) { // <-- now the same nightmare is in both file 
return {
selectByVal: function(el, val) {
console.log('select' + el + val)
try {
return nightmare.select(el, val)
} catch (e) {
return e
}
}
}
}

然后在您的主应用程序上
const bot = require('./utils')(nightmare) // <-- pass the reference

nightmare
.goto(url)
.then(_ => bot.selectByVal('#myDiv', 'myVal'))

关于node.js - 导出/导入Nightmare.js函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com