Testing Firebase function locally, db.collections & snapshots persists of being "empty"

118 views
Skip to first unread message

Nicolas Molinari

unread,
Jul 22, 2022, 10:20:50 AM7/22/22
to Firebase Google Group
Hi everyone.
Days of unsuccessful reseachs.
Debugging step by step this [project]/functions/index.js which is called by rewrite (firebase.json) 
"rewrites": [
{
"source": "/blog/post*",
"function": "dynamicMetaTagsUpdate"
},

The db/snapshots keeps claiming to be empty. Does anyone know what I do wrong ? PS: the goal of this function is for a Flutter web project to be able to handle OpenGrap tags for individual blog/posts

/* eslint-disable max-len */
const BotDetector = require("device-detector-js/dist/parsers/bot");
const fs = require("fs");
const DEBUG_BOT = true;

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();


// Main firebase function called on firebase.json rewrites rules
exports.dynamicMetaTagsUpdate = functions.https.onRequest(async (request, response) => {

let html = fs.readFileSync("../web/index.html", "utf8");
const botDetector = new BotDetector();
const userAgent = request.headers["user-agent"].toString();
const bot = botDetector.parse(userAgent);

if (bot || DEBUG_BOT) {
try {
const { postID } = request.query;
const postUrl = 'http://___________.fr/blog/post?postID=' + postID;

const docRef = db.collection("posts").doc(postID);

const postData = docRef.get().then((snapshot) => {

if (snapshot.exists()) {

const object = {
postUrl: postUrl,
postTitle: 'Article de ____________.fr ref=' + id + ' dans la catégorie ' + snapshot.child("category").val(),
postDescription: snapshot.child("description").val(),
postImageUrl: snapshot.child("postImageUrl").val()[0],
};
return object;
} else {
}
}).catch((error) => {
console.error(error);
});

const htmlData = await postData;
// HTML to return with updated open graph meta tags
html = `
<!doctype html>
<html lang="en">
<head>
<base href="/">
<meta name="theme-color" content="#313131">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="_____">
<link rel="apple-touch-icon" href="icons/Icon-192.p

<title>${htmlData.postTitle}</title>

<meta name="description" content="${htmlData.postDescription}">
<meta name="title" content="${htmlData.postTitle}">

<meta property="og:locale" content="fr_FR" />
<meta property="og:type" content="website" />
<meta property="og:title" content="${htmlData.postTitle}" />
<meta property="og:description" content="${htmlData.postDescription}" />
<meta property="og:image" content="${htmlData.postImageUrl}" />
<meta property="og:url" content="${htmlData.postUrl}" />
<meta name="twitter:card" content="${htmlData.postDescription}" />
<meta name="twitter:creator" content="@______" />
<meta name="twitter:title" content="${htmlData.postTitle}" />
<meta name="twitter:description" content="${htmlData.postDescription}" />

<link rel="manifest" href="manifest.json">
<script src="custom.js"defer></script>
<script src="sharerbox.js"defer></script>

</head>
<body>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('flutter-first-frame', function () {
navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
<script>window.flutterfire_web_sdk_version = '9.9.0';</script>

<script>
const firebaseConfig = {
apiKey: "____________",
authDomain: "__________",
projectId: "________",
storageBucket: "________.appspot.com",
messagingSenderId: "______",
appId: "___________",
measurementId: "__________"
}
firebase.initializeApp(firebaseConfig);
</script>

<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
`;
return response.send(html);
} catch (e) {
console.log(e);
return response.send(html);
}
}
});

Kato Richardson

unread,
Jul 25, 2022, 1:58:10 PM7/25/22
to Firebase Google Group
Hi Nicolas,

How did you determine the snapshot is empty? You don't seem to be logging anything in the else clause that would indicate that. What errors are you running into?

Since there's a lot going on in your code, an obvious way to work through this would be to isolate the problem by removing components until it works. For example, here would be a solid approach to seeing exactly what's breaking down:
  1. Run the Firestore query locally on your machine using node.js
  2. Try simplifying your function to only run that same query and return a result; call that function from HTTP instead of a web page
  3. Work back from there to your current code until it fails again
☼, Kato



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/ea93b60f-4dd8-4e2b-a26e-a3d29c173812n%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages