firebase-functions-snippets icon indicating copy to clipboard operation
firebase-functions-snippets copied to clipboard

Issue with putting cors function into snippet

Open kswain1 opened this issue 5 years ago • 3 comments

I'm confused on where do I exactly place the cors function for the test. Should I place it inside of the function and have another request handler. Or should I place on the function call. Is there anyway you can place all the components inside of the functions. I am a beginner to javascript programming.

exports.ShoePostTest = functions.https.onRequest(cors((request,response) => { // check for requst method if(request.method !== "POST"){ response.status(400).send('Please send a POST request'); return; } console.log(request.body.text) response.send(request.body.text) }))

kswain1 avatar Mar 19 '20 17:03 kswain1

Hi @kswain1, it should be something like this:

const cors = require('cors')({origin: true});

exports.ShoePostTest = functions.https.onRequest((request,response) => {
    cors(request, response, () => {
        // check for requst method
        if(request.method !== "POST"){
            response.status(400).send('Please send a POST request');
            return;
        }
        console.log(request.body.text)
        response.send(request.body.text)
    })    
})

dalenguyen avatar Mar 19 '20 17:03 dalenguyen

Thanks will give that a try.

kswain1 avatar Mar 24 '20 17:03 kswain1

@kswain1 Great, Let me know if it works.

dalenguyen avatar Mar 24 '20 17:03 dalenguyen