我想在AWS Lambda上运行Nightmare JS,但是我的函数总是返回null,并且似乎没有运行任何异步代码。这里是我的代码:AWS上的承诺Lambda函数不解决/返回 - Nightmare JS
exports.handler = (event, context, callback) => {
console.log('starting....')
const Nightmare = require('nightmare')
const nightmare = Nightmare()
console.log('created Nightmare: ', nightmare)
return nightmare
.goto('https://www.myurl.com')
.exists('[data-selector-element]')
.then((exists) => {
console.log('element exists: ', exists)
if (exists) {
return nightmare.click('[data-selector-element]')
.wait(200)
.evaluate(() => {
const title = document.querySelector('h1.header')
return { title }
})
.then((res) => {
context.success(res)
console.log('success:', res)
callback('success: ')
return res
})
} else {
return 'not present'
}
})
}
函数总是返回null,虽然这个过程应该至少需要几秒钟,该功能一般在100毫秒左右结束。前两个控制台日志(大于return nightmare.goto...
)由Lambda注册,但后来的日志不是。
有什么我做错了吗?
context.success不是一个nodejs函数,你的意思是context.succeed? –
也值得一提;一旦你调用context.succeed,该方法将返回并且后续行将不会被执行。 –
好的,谢谢你。是的,我的意思是背景成功。但是,似乎lambda甚至没有越过'return nightmare.goto(...)',所以我不确定这是导致问题的原因。 – otajor