2

I'm using ionic with a google cloud function and when I return data to my application angular defaults to handling it as an error.

Here's my cloud function call back using node: I have already tried adding curly braces to payload and but it didn't help.

function (error, response, body) {
    let payload = parser.toJson(body, parserOptions);
    console.log(payload)
    res.status(200).send(payload);
}

In the console logs for that call back I get:

{ 
    ResponseCode: '0',
    referenceID: '22072017152436718488608295',
    ResponseMessage: 'SUCCESS',
    paymentURL: 'url',
    net_amount: '0',
    invoiceNumber: '0',
    status: '200' 
}

Which is the data I expect to receive in my Angular/Ionic code. Testing this in Postman works too.

Angular Code:

return this.http.post('/api', body, headers)
    .map(res => res.json())
    .subscribe(
        data => {
            console.log('Success')
            console.log(data)
        },
        err => {
            console.log('Error')
            console.log(err.status)
            console.log(err.message)
        }
    )

I get an error status of 0 and error message of null.

robbannn
  • 5,001
  • 1
  • 32
  • 47
shogun000
  • 303
  • 1
  • 2
  • 13
  • could you subscribe to the request directly (i.e. remove the `.map`) and see if that gives you some more information - it could be that it is failing to parse the response for some reason – 0mpurdy Jul 22 '17 at 13:15
  • @0mpurdy Just tried, still getting the same error without the .map – shogun000 Jul 22 '17 at 13:24
  • can you do `console.log(err)` and get any more information? – 0mpurdy Jul 22 '17 at 13:25
  • @0mpurdy Simply an error object with code 0 and message null. – shogun000 Jul 22 '17 at 13:36
  • Ok I think it is answered by [this answer](https://stackoverflow.com/a/27149703/6894075) it's for angularjs but it is probably still a CORS error, hopefully that's enough for you to sort your problem! An angular 2 related question would be [this one](https://stackoverflow.com/q/36768418/6894075) – 0mpurdy Jul 22 '17 at 13:42
  • You're right @0mpurdy, adding a header to my node response worked. Thanks – shogun000 Jul 22 '17 at 13:51

1 Answers1

1

This looks like a CORS error described similarly here for angularjs and with more explanation in this answer for Angular 2

The solution is adding a header server side to handle CORS.

0mpurdy
  • 3,198
  • 1
  • 19
  • 28