我正在使用Firebase可调用云功能,本身可以使用数据,上下文并获取所需的身份验证和数据,也可以将其记录到Firebase控制台。但是,如果我将app.get用作const,则会出现cors错误,无论我尝试什么,似乎都无法修复它。
我尝试添加cors,尝试添加cors选项,尝试了app.use和声明的变量-它们都不起作用。我希望我可以使用一个可调用函数,获取所需的数据并将其设置为app.get请求的变量。这适用于Google Cloud / firebase / firestore。
export const tweets = functions.https.onCall(async(data, context) => {
const userId=context.auth.uid;
console.log('userid',userId)
const userRef=db.doc(`users/${userId}`);
const userSnap=await userRef.get();
const campaignId=userSnap.data().campaignId;
console.log('campaignid', campaignId)
const campaignRef=db.doc(`${campaignId}/social`);
const campaignSnap=await campaignRef.get();
const twitterhandle=campaignSnap.data().twitterHandle;
const tweet=new functions.https.onRequest(app.get('/', function(req, res){
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
cors(req, res, () => {
console.log('fired')
client.get('statuses/user_timeline', {screen_name: twitterhandle, count: 5}, function (err, data ) {
console.log('get started')
if (data) {
console.log('if data')
console.log(JSON.stringify(data))
return res.json(data)
}
else {
console.log({ error: "this is error: " + JSON.stringify(err) })
return
}
});
console.log(tweet)
})
}
)
)
return tweet;
});
我希望能够将tweet作为JSON数据返回,如果我将其分开工作的话,但这不是我想要的。
答案 0 :(得分:0)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Twitter = require('twitter');
admin.initializeApp(functions.config().firebase);
const twittercredentials=functions.config().twitter;
//twittercredentials
const client = new Twitter(
twittercredentials)
const db = admin.firestore();
//setup authentication and get the users twitter id
//begins the cloud function
export const tweets = functions.https.onCall(async(data, context) => {
const userId=context.auth.uid;
const userRef=db.doc(`users/${userId}`);
const userSnap=await userRef.get();
const campaignId=userSnap.data().campaignId;
const campaignRef=db.doc(`${campaignId}/social`);
const campaignSnap=await campaignRef.get();
const twitterhandle=campaignSnap.data().twitterHandle;
const tweeted=function_get(twitterhandle)
return tweeted
})
function function_get(twitterhandle) {
//setup seperate function to get tweets and return on callable function
client.get('statuses/user_timeline', {screen_name: twitterhandle, count: 5}, function (err, tweets, res ) {
if (tweets) {
console.log(JSON.stringify(tweets))
return tweets
}
else {
console.log({ error: "error: " + JSON.stringify(err) })
return
}
})
}