Reputation: 602
I am trying to send emails from a firebase
function that I call from my vue
app. My firebase function looks like:
const functions = require('firebase-functions');
const admin = require("firebase-admin")
const nodemailer = require('nodemailer');
admin.initializeApp()
//google account credentials used to send email
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'xxxxxxxxxx'
}
});
exports.sendMail = functions.https.onRequest((req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
const mailOptions = {
from: `•••••••••@gmail.com`,
to: req.body.email,
subject: 'contact form message',
html: `<h1>Order Confirmation</h1>
<p>
<b>Email: </b>"hello<br>
</p>`
};
return transporter.sendMail(mailOptions, (error, data) => {
console.log("here")
if (error) {
return res.send({"data": error.toString()});
}
data = JSON.stringify(data)
return res.send({"data": `Sent! ${data}`});
});
});
I removed the user and pass values here. They have the correct value in my code. The vue
method I use to call this function looks like:
sendEmail(){
let sendMail = firebase.functions().httpsCallable('sendMail');
sendMail(
{
email: '[email protected]'
}
).then(
result => {
console.log(result.data)
}
)
}
When this function is called I get the following error in the console:
Uncaught (in promise) Error: Response is not valid JSON object.
at new e (index.cjs.js:58)
at t.<anonymous> (index.cjs.js:543)
at u (tslib.es6.js:100)
at Object.next (tslib.es6.js:81)
at s (tslib.es6.js:71)
Any help would be appreciated. Thanks
Upvotes: 7
Views: 10062
Reputation: 111
It happened to me that I changed the function name from name1
to name2
and in my function calling name1
the response was always "Response is not valid JSON object."
It happened to me in Flutter
Upvotes: 0
Reputation: 5837
I was getting this because I forgot to connect the emulator:
import { connectFunctionsEmulator, getFunctions } from 'firebase/functions'
...
const functions = getFunctions(app)
connectFunctionsEmulator(functions, '127.0.0.1', 5001)
Note for me using 'localhost' caused an internal error
Upvotes: 2
Reputation: 1881
Just incase this helps anyone Googling the issue, I got the "Response is not valid JSON object." error because I didn't specify the correct region in my client and my function wasn't hosted in the default us region. I'm writing a react-native firebase app so I had to change:
const result = await firebase.app().functions().httpsCallable("nameGoesHere");
To:
const result = await firebase.app().functions("europe-west1").httpsCallable("nameGoesHere");
Upvotes: 15
Reputation: 96
The issue might be with the handler. You're using functions().httpsCallable('your_name')
but the handle of the functions is onRequest
. According to the documentation, there should be onCall
function handler.
Something like:
exports.sendMail = functions.https.onCall((data) => { your_code_here });
Docs - https://firebase.google.com/docs/functions/callable
Upvotes: 3
Reputation: 4952
I can't tell if this is the case from the code, but this always happens to me when I export a function in one file and then forget to include it in the index file.
Upvotes: 1
Reputation: 6480
You must a return a valid JSON
result like this:
return res.status(200).json({});
Here the JSON is
{}
but it could be for instance something like {"status": "success}
.
Upvotes: 6