Reputation: 91
I'm using google firebase cloud functions.
When accessing my cloud function from client, I get the below HttpError
SyntaxError: Unexpected token S in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:13445:51)
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:13121:35)
at Object.onInvokeTask (http://localhost:4200/vendor.js:71621:33)
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:13120:40)
at Zone.runTask (http://localhost:4200/polyfills.js:12888:51)
at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:13203:38)
at invokeTask (http://localhost:4200/polyfills.js:14361:18)
at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:14398:25)
text: "Sended"
Below is my server code,
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
.....
return transporter.sendMail(mailOptions, (erro, info) => {
if (erro) {
return res.send(erro.toString());
}
return res.send('Sended');
});
})
});
});
Please tell me what i'm doing wrong here ?
Upvotes: 1
Views: 319
Reputation: 237
Modify the code like this:
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
.....
return transporter.sendMail(mailOptions, (erro, info) => {
if (erro) {
return res.status(500).json({"message":erro.toString()});
}
return res.status(200).json({"message":"Sent"})
});
})
});
It's a good practice to specify HTTP status codes in the response of your requests and also send standard JSON data through it. I myself always send a "message" field for such requests.
Upvotes: 1