简体   繁体   English

在 facebook 上分享一个页面,使用 href 和 javascript 抛出错误

[英]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);在我尝试通过 jquery 添加 og 元标记信息之前,但它不起作用,所以我改变了我的方法,将云功能与节点一起使用,使用 express,节点返回一个填充了所有元标记的页面,但是当我点击按钮时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.我收到一个错误,url 只是来自 location.search function 的字符串。 Anyway the thing is if I copy the url of the event a paste on facebook manually, everything goes right.无论如何,如果我手动复制事件的 url 并粘贴到 facebook 上,一切正常。 I'll show you the pictures and the code我给你看图片和代码在此处输入图像描述 在此处输入图像描述

this is the code from the head of index.ejs这是 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这是我的云函数 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)

by the way, when I create the object static, it works, but when it comes from the database it doesn't.顺便说一句,当我创建 object static 时,它可以工作,但是当它来自数据库时,它不会。 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并且在我的云功能控制台上也得到这个错误,这真的很奇怪,因为它说是 null,但是当我在 html 的正文中打印时它工作正常,当我打开谷歌控制台并检查头部元素时,即使它说它是 null,有关事件的所有信息都设置在 og 元标记中在此处输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM