Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud functions 傀儡谷歌云功能发布/订阅触发器可以';t打开浏览器_Google Cloud Functions_Puppeteer_Google Cloud Pubsub - Fatal编程技术网

Google cloud functions 傀儡谷歌云功能发布/订阅触发器可以';t打开浏览器

Google cloud functions 傀儡谷歌云功能发布/订阅触发器可以';t打开浏览器,google-cloud-functions,puppeteer,google-cloud-pubsub,Google Cloud Functions,Puppeteer,Google Cloud Pubsub,我试图在GCP中创建一个木偶演员函数,它可以由发布/订阅消息触发。该函数是可调用的,但行为不符合预期,并且在浏览器尝试初始化时抛出超时错误。触发器可能使用不同于HTTP触发器的NodeJS环境吗 我也是NodeJS的新手,所以如果问题显而易见,我会提前道歉 我已经为函数创建了一个HTTP触发器,它的行为与预期的一样。在创建Cloud函数时,我将下面的Puppeter函数复制/粘贴到index.js中,但为了清楚起见,在示例中将其分开,即两个触发器运行相同的函数 async function wr

我试图在GCP中创建一个木偶演员函数,它可以由发布/订阅消息触发。该函数是可调用的,但行为不符合预期,并且在浏览器尝试初始化时抛出超时错误。触发器可能使用不同于HTTP触发器的NodeJS环境吗

我也是NodeJS的新手,所以如果问题显而易见,我会提前道歉

我已经为函数创建了一个HTTP触发器,它的行为与预期的一样。在创建Cloud函数时,我将下面的Puppeter函数复制/粘贴到index.js中,但为了清楚起见,在示例中将其分开,即两个触发器运行相同的函数

async function wrapper() {
    try {
        const result = await scrapeUglyWebsite(); 
        if(results) {
            console.log('Suzanne Collins takes pictures with rats.');
        } else {
            console.log("Suzzane Collins doesn't take pictures with rats.");
        };
    } catch (err) {
        console.log(err.toString());
    }
}
木偶演员功能

const puppeter=require('puppeter');
ScrapeUGLY网站=()=>{
返回新承诺(异步(解析、拒绝)=>{
等待木偶师({
无头:是的,
args:['--无沙盒']
})
。然后(异步(浏览器)=>{
const page=wait browser.newPage();
等待页面。转到('http://suzannecollinsbooks.com/“,{waitUntil:'load',超时:0})
。然后(异步()=>{
//等待内容加载
wait page.waitForFunction('document.body!==null&&document.body.innerText.includes(\'Jon Scieszka\'));
//评估页面内容
const dom_eval=wait page.evaluate(()=>document.body.innerText.includes(“这是我和老鼠的照片”);
等待浏览器关闭();
解决(dom_eval);
});
}).catch((错误)=>{
拒绝(错误);
});
});
};
HTTP触发器-index.js

exports.cloudFunctionTest=(请求、恢复)=>{
(网址)
。然后((结果)=>{
如果(结果){
res.send(‘苏珊娜·柯林斯与老鼠合影’);
}否则{
res.send(“Suzzane Collins不与老鼠拍照。”);
};
})
.catch((错误)=>{
res.send(err.toString());
});
发布/订阅Trgger-index.js

exports.cloudFunctionTest=(数据、上下文)=>{
(网址)
。然后((结果)=>{
如果(结果){
log('Suzanne Collins与老鼠合影');
}否则{
log(“Suzzane Collins不与老鼠拍照。”);
};
})
.catch((错误)=>{
log(err.toString());
});
};
package.json

{
  "name": "test",
  "version": "0.0.1",
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "puppeteer": "^1.6.0"
  }
}
HTTP触发器的行为符合预期结果

Suzanne Collins takes pictures with rats.
Pub/Sub触发器抛出以下错误,没有输出

TimeoutError: Timed out after 30000 ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r662092

我知道这很晚了,但出现TimeoutError的原因是云函数不会自动等待异步任务完成。因此在
exports.cloudFunctionTest
ScrapuglyWebsite()中调用了
,但函数没有等待承诺的实现,因此程序终止。因此出现错误

有关后台函数如何在NodeJs中工作的更多信息

为了让函数等待
scrapeUglyWebsite()
,您需要返回一个承诺,该承诺在
scrapeUglyWebsite()
且生成的代码完成时完成

就我个人而言,我只需将当前正在导出的函数中的代码包装到另一个异步函数中,然后返回包装函数的承诺,就可以让它正常工作

async function wrapper() {
    try {
        const result = await scrapeUglyWebsite(); 
        if(results) {
            console.log('Suzanne Collins takes pictures with rats.');
        } else {
            console.log("Suzzane Collins doesn't take pictures with rats.");
        };
    } catch (err) {
        console.log(err.toString());
    }
}
然后在要导出的函数中:

exports.cloudFunctionTest = (data, context) => {
    return wrapper();
};