Error: Received unknown parameter: source - javascript

I'm making a react-redux app with firetore as database. Now, I wanted to use firebase cloud functions for handling stripe payments.
Here is the cloud function "createSubscription":
exports.createSubscription = functions.database
.ref("/teachers/{userId}/pro-membership/token")
.onWrite((event, context) => {
const tokenId = event.after.val();
const userId = context.params.userId;
if (!tokenId) throw new Error("Token Missing");
return admin
.database()
.ref(`teachers/${userId}`)
.once("value")
.then(snapshot => snapshot.val())
.then(user => {
console.log(user);
return stripe.subscriptions.create({
customer: user.customerId,
source: tokenId, **// Here is the error occuring**
items: [
{
plan: "pro-membership"
}
]
});
})
.then(sub => {
admin
.database()
.ref(`teachers/${userId}/pro-membership`)
.update({
status: "active"
});
})
.catch(err => {
console.log("ERRor", err);
});
});
Below is the error information from cloud function's logs:

source is not a valid parameter on a stripe.subscriptions.create request, see https://stripe.com/docs/api/subscriptions/create
Try updating the customer first, adding the token, https://stripe.com/docs/api/customers/update, then create a subscription!

Related

Javascript :( 500 Internal server error) in the postman

Material that i used
windows10
Firebase (fire-store)
postman
Javascript , express
situation
I tried to sign up to firebase and make data on the firestore. but the internal error occurs. Does someone confirm my code?
index.js
// Signup route
app.post('/signup', (req,res) => {
const newUser = {
email: req.body.email,
password: req.body.password,
confirmPassward: req.body.confirmPassword,
handle: req.body.handle
};
//TODO validate data
let token, userId;
db.doc(`/users/${newUser.handle}`)
.get()
.then(doc => {
if(doc.exists) {
return res.status(400).json({ hanldle: 'this hanlde is already taken'});
}else {
return firebase()
.auth()
.createUserWithEmailAndPassword(newUser.email, newUser.password);
}
})
.then((data) => {
userId = data.user.uid;
return data.usergetIdToken()
})
.then( ( idToken ) => {
token = idToken ;
const userCredentials = {
handle: newUser.handle,
email: newUser.email,
createdAt: new Date().toISOString(),
userId
};
return db.doc(`/users/${newUser.handle}`).set(userCredentials);
})
.then(() => {
return res.status(201).json({ token });
})
.catch(err => {
if(err.code === 'auth/email=already-in-use') {
return res.status(400).json({ email: 'email is alread is used '})
} else {
return res.status(500).json({ err : err.code });
}
});
});
exports.api = functions.https.onRequest(app);
My console visual studio
! functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i functions: Beginning execution of "api"
! External network resource requested!
- URL: "http://169.254.169.254/computeMetadata/v1/instance"
- Be careful, this may be a production service.
! External network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
- Be careful, this may be a production service.
i functions: Finished "api" in ~9s
i functions: Beginning execution of "api"
i functions: Finished "api" in ~8s
Postman

React with Cloud Firestore

Im trying to switch from realtime database to cloud firestore in my react app.
In my firebase.js, I have a definition of users as set out below.
class Firebase {
constructor() {
app.initializeApp(config).firestore();
this.auth = app.auth();
// this.db = app.firebase.database()
this.db = app.firestore();
}
doCreateUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password);
doSignInWithEmailAndPassword = (email, password) =>
this.auth.signInWithEmailAndPassword(email, password);
doSignOut = () =>
this.auth.signOut();
doPasswordReset = email =>
this.auth.sendPasswordResetEmail(email);
doPasswordUpdate = password =>
this.auth.currentUser.updatePassword(password);
// *** User API ***
user = uid => this.db.ref(`users/${uid}`);
users = () => this.db.ref('users');
}
This worked to get users when I was using realtime database, but now I get an error message that says:
this.db.ref is not a function
Why can't I reference this.db anymore and how do I navigate the cloud firestore docs to find the equivalent?
I have seen this post which recommends keeping the realtime database for some things. Is there a list of things that cloud firestore can't do and is getting a list of users one of them?
I have seen this documentation and tried:
user = uid => this.db.DocumentReference(`users/${uid}`);
users = () => this.db.DocumentReference('users');
This produces the same error message as using this.db.ref
There is no ref() in firestore, if you want to access a document you need to use the method document():
db.collection("users").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
Please check the following doc for firestore :
https://firebase.google.com/docs/firestore/quickstart

Firebase Cloud Functions - How to call another function and return object

I've created a callable function from my Angular component. My angular component calls the createUser function and successfully returns the userRecord value.
However, what I would like to do is call another cloud function called createUserRecord. I'm not overly familiar with promises and what needs to be returned where in this particular scenario.
Below are my two cloud functions. How would I go about calling createUserRecord upon the success of createUser?
export const createUser = functions.https.onCall(async (data, context) => {
console.log('data = ', data);
return admin.auth().createUser({
email: data.email,
password: data.password,
}).then(function (userRecord) {
return userRecord
})
.catch(function (error) {
return error;
console.log('Error creating new user:', error);
});
});
export const createUserRecord = functions.auth.user().onCreate((user, context) => {
const userRef = db.doc(`users/${user.uid}`);
return userRef.set({
email: user.displayName,
createdAt: context.timestamp,
nickname: 'bubba',
})
});
Update
This is a version I produced where I merged the two functions together. This does produce the expected result of creating and account and then writing to firestore. However, it does feel a little 'off' due to the fact that it doesn't return a value to the client.
export const createUser = functions.https.onCall(async (data, context) => {
console.log('data = ', data);
return admin.auth().createUser({
email: data.email,
password: data.password,
}).then(function (userRecord) {
const userRef = db.doc(`users/${userRecord.uid}`);
return userRef.set({
email: data.email,
name: data.name,
})
})
.catch(function (error) {
return error;
console.log('Error creating new user:', error);
});
});
Angular Callable Function
The sanitizedMessage console log will return undefined.
addUser() {
const createUser = firebase.functions().httpsCallable('createUser');
const uniquePassword = this.afs.createId();
createUser({
email: this.userForm.value.email,
password: uniquePassword,
name: this.userForm.value.name,
}).then((result) => {
// Read result of the Cloud Function.
var sanitizedMessage = result.data.text;
console.log('sanitizedMessage = ', sanitizedMessage);
}).catch((error) => {
var code = error.code;
var message = error.message;
var details = error.details;
console.log('error = ', error);
});
}
If you want to create a record in Firestore upon user creation you can very well do that within a unique Cloud Function. The following code will do the trick, making the assumption that you want to write to the users Firestore collection.
const FieldValue = require('firebase-admin').firestore.FieldValue;
...
export const createUser = functions.https.onCall((data, context) => {
console.log('data = ', data);
return admin
.auth()
.createUser({
email: data.email,
password: data.password
})
.then(userRecord => {
return admin.firestore().collection('users')
.doc(userRecord.uid)
.set({
email: userRecord.displayName,
createdAt: FieldValue.serverTimestamp(),
nickname: 'bubba'
});
})
.then(() => {
return {
result: 'Success'
};
})
.catch(error => {
//Look at the documentation for Callable Cloud Functions to adapt this part:
//https://firebase.google.com/docs/functions/callable?authuser=0
});
});
"Is there any particular reason not to chain functions in CF's?" ?
As explained in the documentation, "Cloud Functions can be associated with a specific trigger". You can "chain" Cloud Functions by creating the corresponding triggers, for example, creating a doc in Firestore in one CF (Callable Function for example), that would trigger another CF that respond to a Firestore trigger). Having said that, in most cases you can probably cover a lot of needs in a unique Cloud Function, by chaining promises, instead of chaining Cloud Functions.
Finally, I would not recommend to call an HTTP Cloud Function from within a Cloud Function because (IMHO) HTTP Cloud Functions are more designed to be called by an external consumer (I even don't know if this would work).
It would be interesting to have Firebasers opinion on that!

Firebase Function/ Express: Can't set headers after they are sent

I've mostly utilised the Hapi framework to build RESTful APIs. For this project I'm using Express and I'm a bit lost as to why this is happening.
When I test the POST endpoint using Postman, the first request is fine, but I would get an error when I make the second request.
Error: Can't set headers after they are sent.
The code for the route handler is below:
const login = (req, res) => {
const validation = authScema.loginPayload.validate(req.body)
if (validation.error) {
return res.status(400).send(validation.error.details[0].message)
}
const { email, password } = req.body
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.catch(error => {
// Handle Errors here.
if (error) {
return res.status(400).send('Invalid login details.')
}
})
firebase.auth().onAuthStateChanged(user => {
if (user) {
const userObject = {
email: user.email,
uid: user.uid
}
const token = jwt.sign(userObject, secret)
return res.status(200).send(token)
}
})
}
I don't understand why headers are resent since in every branch, I return. It should have exited the function, right?
Turns out, signInWithEmailAndPassword
is a promise that returns the user in the happy path
So, the following is the final code:
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(user => {
const userObject = {
email: user.email,
uid: user.uid
}
const token = jwt.sign(userObject, secret)
res.status(200).json({ token })
})
.catch(error => {
if (error) {
res.status(400).json({ message: 'Invalid login details.' })
}
})
The onOnAuthStateChanged is not necessary in this case.

Firebase Firestore FCM message sending issue

My code :
exports.fcmSend = functions.firestore.document('messages/{userId}').onCreate(event => {
console.log("fcm send method");
const message = event.data.data();
const userId = event.params.userId;
const token_id = 'asdfsadfdsafds';
let token = "";
const payload = {
notification: {
title: "Test",
body: "Test",
icon: "https://placeimg.com/250/250/people"
}
};
db.collection('fcmTokens').doc('token_id').get().then((doc) => {
console.log(doc.id, '=>', doc.data());
const data = doc.data();
token = data.token;
console.log("token", token);
})
.then(() => {
return event.data.ref.set({"title": "hello"}).sendToDevice(token, payload);
})
.catch((err) => {
console.log('Error getting documents', err);
return err;
});
});
Error :
Error getting documents TypeError:
event.data.ref.set(...).sendToDevice is not a function
at db.collection.doc.get.then.then (/user_code/index.js:117:50)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
There are two separate Firebase products involved here:
Cloud Firestore, where you stroke the FCM tokens for a user.
the Cloud Messaging Admin SDK, which you use to send notifications to a user.
The sendToDevice method exists on the Admin SDK for Cloud Messaging, not on a Firestore database reference where you're trying to invoke it.
To fix the problem you'll first need to import the Admin SDK into your index.js:
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
Then you modify your functions for step 1 and 2. It should look something like this:
// Load the tokens from Firestore
db.collection('fcmTokens').doc('token_id').get().then((doc) => {
console.log(doc.id, '=>', doc.data());
const data = doc.data();
token = data.token;
console.log("token", token);
const payload = {
notification: {
title: 'hello',
}
};
return admin.messaging().sendToDevice(token, payload)
})
.catch((err) => {
console.log('Error getting documents', err);
return err;
});

Categories