Search code examples
angulargetfaunadb

How can I access to the content of my Get request using lambda function


I need to get object instance stored in my faunaDB DB, but I can't get his content in my front

I tried to identify problem with console logs but...

Here is my lambda function

/* code from functions/todos-read.js */
import faunadb from 'faunadb'

const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET
})

exports.handler = (event, context, callback) => {
  const id = '234316534878568967'
  console.log(`Function 'todo-read' invoked. Read id: ${id}`)
  return client.query(q.Get(q.Ref(q.Class("missions"), "234316534878568967")))
  .then((response) => {
    console.log("success", response)
    return callback(null, {
      statusCode: 200,
      body: JSON.stringify(response)
    })
  }).catch((error) => {
    console.log("error", error)
    return callback(null, {
      statusCode: 400,
      body: JSON.stringify(error)
    })
  })
}

my function in my angular service:

  readById = () => {
    return fetch('/.netlify/functions/mission-read-by-id').then((response) => {
      console.log(response);
      return response.json();
    });
  }

then I assigned this function to a var in my component, with a console.log

this.missionData = this.missionService.readById();
console.log(this.missionData);

result of response in console:

[BACK] [LAMBDA] Request from ::ffff:127.0.0.1: GET /mission-read-by-id
[BACK] [LAMBDA] Function 'todo-read' invoked. Read id: 234316534878568967
[BACK] [LAMBDA] success { ref: Ref(Class("missions"), "234316534878568967"),
[BACK] [LAMBDA]   ts: 1559720511260000,
[BACK] [LAMBDA]   data:
[BACK] [LAMBDA]    { consultant: 'sd',
[BACK] [LAMBDA]      consultantEmail: '[email protected]' } }
[BACK] [LAMBDA] Response with status 200 in 256 ms.

result of console.log in component:

{…}
​
__zone_symbol__state: true
​
__zone_symbol__value: {…}

​​data: {…}
​​​
client: "dvs"
​​​
clientEmail: "[email protected]"

<prototype>: Object { … }
​​
ref: Object { "@ref": {…} }
​​
ts: 1559720511260000
​​
<prototype>: Object { … }
​
<prototype>: Object { then: then(), catch: catch(), finally: finally(), … }

I don't understand how to get my object, then if you can explain me... Thanks a lot


Solution

  • The readByIdis returning a Promise so, you need to change your code to use this:

    this.missionService.readById().then(missionData => console.log(missionData))
    // or using async/await
    this.missionData = await this.missionService.readById();
    console.log(this.missionData);