首页 > 解决方案 > 使用 href 和 javascript 抛出错误在 facebook 上共享页面

问题描述

我有一个从实时数据库加载其内容的页面,它是一个事件页面,因此图像、标题等根据查询参数而有所不同。在我尝试通过 jquery 添加 og 元标记信息但它不起作用之前,所以我改变了我的方法以使用带有节点的云函数,使用 express,节点返回一个填充了所有元标记的页面但是当我单击按钮时与该 facebook url 共享页面 window.open("https://www.facebook.com/sharer/sharer.php?u=https://apptcc-6f556.firebaseapp.com/evento"+url); 我收到一个错误,url 只是 location.search 函数中的一个字符串。无论如何,如果我手动将事件的 url 复制到 facebook 上,一切都会正常进行。我给你看图片和代码 在此处输入图像描述 在此处输入图像描述

这是 index.ejs 负责人的代码

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


    

这是我的云函数 index.js 中的代码

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)

顺便说一句,当我创建静态对象时,它可以工作,但是当它来自数据库时,它不会。并且在我的云功能控制台上也得到这个错误,这真的很奇怪,因为它说是空的,但是当我在 html 的正文中打印时它工作正常,当我打开谷歌控制台并检查头部元素时,即使它说它为空,有关事件的所有信息都设置在 og 元标记中 在此处输入图像描述

标签: javascriptnode.jsfacebookgoogle-cloud-functionsfacebook-opengraph

解决方案


没关系,我发现了错误,发生的情况是,当我写我的 url 时,我使用“&”字符来分隔参数,而发生的是 facebook 的 api 或其他任何东西,无法理解 & 字符,所以我刚刚使用了javascript函数encodeURIComponent并且它有效


推荐阅读