Search code examples
javascriptnode.jsfacebookgoogle-cloud-functionsfacebook-opengraph

share a page on facebook using href and javascript throwing error


I have a page that load its content from the realtime-database, it's a event page, so the image,title and so on are different based on the query params. before I tried adding the og meta tag info through jquery but it didn't work, so I changed my approach to use cloud functions with node, using express, node returns a page with all the meta tags filled but when I click on the button to share the page with that facebook url window.open("https://www.facebook.com/sharer/sharer.php?u=https://apptcc-6f556.firebaseapp.com/evento"+url); I get an error, url is just a string from the location.search function. Anyway the thing is if I copy the url of the event a paste on facebook manually, everything goes right. I'll show you the pictures and the code enter image description here enter image description here

this is the code from the head of index.ejs

<meta property="og:image" content="<%= evento.caminho %>">
<meta property="og:title" content="<%= evento.titulo %>">
<meta property="og:description" content="<%= evento.descricao %>">


    

and this my code from my index.js of my cloud functions

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require("express");
const app = express();
admin.initializeApp();
function getEvento(pais,categoria,id){
    return adminRef.child(`eventos/${pais}/${categoria}/${id}`).once("value")
    .then(evento=>evento.val());
}

app.get("/evento",(req,res)=>{
    const evento = {
        titulo:"teste",
        id:"-MCxaVd4JwRcbdK174Sn",
        descricao:"dasd",
        caminho:"https://firebasestorage.googleapis.com/v0/b/apptcc- 
        6f556.appspot.com/o/eventos%2FBrazil%2FMG%2FEspinosa%2FVLxS4OQN4CaTK89A7f6853wEX7P2%2F-M7ZG- 
        5Yg9u41UC_Pda-?alt=media&token=e7f28393-6c0a-4095-8c46-5485ed82824c"
    }
    return res.render("index.ejs",{evento});


    getEvento(req.query.pais,req.query.categoria,req.query.id)
    .then(evento=>{
        return res.render("index.ejs",{evento})
    })
    .catch(error=>{
        console.log(error);
        res.send(error);
    })

 })

 exports.app = functions.https.onRequest(app)

by the way, when I create the object static, it works, but when it comes from the database it doesn't. And also get this error on my cloud functions console which is really weird because it says is null, but when I print in the body of the html it works fine, and when I open google console and check the head elements, even though it says it's null, all the information about the event is set in og meta tags enter image description here


Solution

  • Never mind, I found the error, what happens is, when wrote my url I was using the "&" character to separate the parameters, and what happens is facebook's api or whatever that thing is, can;t understand the & character, so I just used the javascript function encodeURIComponent and it worked