I'm testing a very simple implementation as described on FB docs (), and it's not working.
Here's my Firebase Function, deployed to cloud:
exports.getRecSkills = functions.https.onCall((data, context) => {
return {text: data.text};
});
...and my client call (after initializing FB):
var getRecSkills = firebase.functions().httpsCallable('getRecSkills');
getRecSkills({text: '123'}).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
I get a CORS header related issue but in the docs, it doesn't mention the need for CORS... am I missing something?
Some notes:
- I've been able to execute other Firebase Functions (i.e. HTTPS, Database) so I don't think it's me setting up Firebase wrong.
- Updated to latest Firebase, so don't think that's an issue either.
- Gives me an "internal" error, which the API docs aren't helpful, other than "something is seriously wrong".
- I can't seem to get the function to work (it keeps giving me 400-errors) when testing locally via the shell, even though I got it to work with any other database and https functions
Been struggling with this for quite some time... Please help!
I'm testing a very simple implementation as described on FB docs (https://firebase.google./docs/functions/callable), and it's not working.
Here's my Firebase Function, deployed to cloud:
exports.getRecSkills = functions.https.onCall((data, context) => {
return {text: data.text};
});
...and my client call (after initializing FB):
var getRecSkills = firebase.functions().httpsCallable('getRecSkills');
getRecSkills({text: '123'}).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
I get a CORS header related issue but in the docs, it doesn't mention the need for CORS... am I missing something?
Some notes:
- I've been able to execute other Firebase Functions (i.e. HTTPS, Database) so I don't think it's me setting up Firebase wrong.
- Updated to latest Firebase, so don't think that's an issue either.
- Gives me an "internal" error, which the API docs aren't helpful, other than "something is seriously wrong".
- I can't seem to get the function to work (it keeps giving me 400-errors) when testing locally via the shell, even though I got it to work with any other database and https functions
Been struggling with this for quite some time... Please help!
Share Improve this question edited Jun 26, 2018 at 8:16 Jbbae asked May 6, 2018 at 2:45 JbbaeJbbae 1,03810 silver badges20 bronze badges 8- 1 What version of the client and node SDKs are you using? – Doug Stevenson Commented May 6, 2018 at 3:15
- I'm using the latest for both - FB client (v4.13.1), and FIrebase admin (v5.12.0) – Jbbae Commented May 6, 2018 at 18:35
- What version of firebase-functions are you using on the server? – Doug Stevenson Commented May 6, 2018 at 18:54
- I am deploying my functions to Firebase with v1.0.2 (the latest), which is what I assume you're asking? – Jbbae Commented May 8, 2018 at 1:15
- 1 I see that you forgot to initialize your config with projectId.. your URL has 'undefined' on it – Pietro Coelho Commented May 10, 2018 at 17:02
3 Answers
Reset to default 2To get rid of your CORS
error, make sure your firebase.json
has the following headers
:
"hosting": [
{
"headers": [
{
"source": "**",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}
]
}
]
If you're running on Firebase Emulator on local device, make sure you have the following after initializing your Firebase Functions, otherwise your local device will still be calling the remote the Firebase Function and you'll hit the CORS
error again:
if (window.location.hostname === "localhost") {
console.log("localhost detected!");
firebase.functions().useFunctionsEmulator('http://localhost:5001');
};
I had the same problem just recently but solved it after including my "projectId" in my config object. Below is a code snippet of the Firebase config object for Javascript. Make sure all fields have been filled in your config object and it should solve your undefined issue.
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.",
databaseURL: "https://<DATABASE_NAME>.firebaseio.",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.",
messagingSenderId: "<SENDER_ID>",
};
If you have CORS
issues and you are using express
in order to expose the API functions you have to allow cors:
import * as cors from 'cors';
import * as express from 'express';
const corsHandler = cors({origin: true});
const app = express();
app.use(corsHandler);
app.post('/createUser', async (request, response) => {
await createUser(request, response);
});
exports.api = functions.https.onRequest(app);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742252667a4409445.html
评论列表(0条)