我在节点8运行时下运行google云函数,为侦听器分配了2g。听众:没有木偶者的作品;木偶剧:在请求中使用时的作品;
但是我想让木偶师在我的听众里面工作,这使得我已经有效的听者抛出了一个错误。
我读过很多文档,但没有一部把木偶技师整合到听者的体内。
编辑:我还尝试将返回类型更改为有效格式,但没有效果。
我的有效听众
exports.listen = functions.firestore
.document('/request/{id}')
.onWrite((change, context) => {
// Grab the data from the original function trigger
const document = await change.after.data()['data'];
// Dummy return of what will eventually be given
return change.after.ref.set(
{
results: [
{ title: 'New Title', Country: 'Lala Land' },
{ title: 'New Job', Country: 'Asgardia' }
]
},
{ merge: true }
);
});有效Puppeteer函数
const express = require('express');
const functions = require('firebase-functions');
const puppeteer = require('puppeteer');
const app = express();
// Runs before every route. Launches headless Chrome.
app.all('*', async (req, res, next) => {
// Note: --no-sandbox is required in this env.
// Could also launch chrome and reuse the instance
// using puppeteer.connect()
res.locals.browser = await puppeteer.launch({
args: ['--no-sandbox']
});
next(); // pass control to next route.
});
// Handler to take screenshots of a URL.
app.get('/screenshot', async function screenshotHandler(req, res) {
const url = req.query.url;
if (!url) {
return res.status(400).send(
'Please provide a URL. Example: ?url=https://example.com');
}
const browser = res.locals.browser;
try {
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle2'});
const buffer = await page.screenshot({fullPage: true});
res.type('image/png').send(buffer);
} catch (e) {
res.status(500).send(e.toString());
}
await browser.close();
});无效听者将木偶师更改为最小需求
exports.listen = functions.firestore
.document('/request/{id}')
.onWrite((change, context) => {
const document = change.after.data()['data'];
async function benchmark() {
const browser = await puppeteer.launch({
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto('http://picocms.org/', {
waitUntil: 'networkidle2'
});
const content = await page.content();
return content;
}
return change.after.ref.set(
{
// results: [
// { title: 'New Title', Country: 'Lala Land' },
// { title: 'New Job', Country: 'Asgardia' }
// ]
results: benchmark()
},
{ merge: true }
);
});日志中生成的错误
Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript object (found in field results).
at Object.validateUserInput (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:312:15)
at validateDocumentData (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:622:26)
at WriteBatch.set (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:242:9)
at DocumentReference.set (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:337:27)
at exports.listen.functions.firestore.document.onWrite (/srv/index.js:57:27)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:114:23)
at /worker/worker.js:825:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)发布于 2019-04-10 13:16:51
他们确实应该为这个用途制作文档,但我找到了自己的解决方案,我只需要在帖子中使用木偶师,或者得到请求,因为您将无法访问(请求,响应) params。
对于任何想要以自己的方式使用它的人来说,这里是一个最小的可行的解决方案。
注意:我将运行时升级到Node 10 (beta),分配了2G。
const functions = require('firebase-functions');
const puppeteer = require('puppeteer');
const app = require('express')();
const { db } = require('./util/admin');
// Runs before every route. Launches headless Chrome.
app.all('*', async (req, res, next) => {
// Note: --no-sandbox is required in this env.
// Could also launch chrome and reuse the instance
// using puppeteer.connect()
res.locals.browser = await puppeteer.launch({
args: ['--no-sandbox']
});
next(); // pass control to next route.
});
app.post('/create', async (req, res) => {
const request = {
data: req.body.data,
created: new Date().toISOString()
};
const browser = res.locals.browser;
const page = await browser.newPage();
await page.goto('http://picocms.org/', {
waitUntil: 'networkidle2'
});
const content = await page.content();
request.data = JSON.stringify(content);
await browser.close();
db.collection('request')
.add(request)
.then(doc => {
return res.json({
message: `[success] ${doc.id} Generated`
});
})
.catch(e => {
res.status(500).json({
error: `[failed] No Request Was Saved _> ${e}`
});
});
});
exports.api = functions.https.onRequest(app);发布于 2019-04-10 14:47:03
您的benchmark()函数是async,因此在调用时将返回一个承诺。
因此,您正在尝试在这里存储一个承诺:
return change.after.ref.set(
{
results: benchmark() // This is a promise
},
{ merge: true }
);您希望将承诺中的解析值存储起来。您必须将函数更改为async,然后在benchmark()调用之前添加一个await。然后,您的数据将成为一个“普通的JavaScript对象”,并可以序列化和存储。
https://stackoverflow.com/questions/55612870
复制相似问题