2

我需要将对象实例存储在我的动物区数据库中,但我无法将他的内容放在我的前面

我试图找出控制台日志的问题,但是......

这是我的 lambda 函数

/* 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)
    })
  })
}

我在我的角度服务中的功能:

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

然后我将此函数分配给我的组件中的一个var,带有一个console.log

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

控制台响应结果:

[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: 'sdq@gg.com' } }
[BACK] [LAMBDA] Response with status 200 in 256 ms.

console.log 组件的结果:

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

​​data: {…}
​​​
client: "dvs"
​​​
clientEmail: "sdq@gg.com"

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

我不明白如何得到我的对象,那么如果你能解释我......非常感谢

4

2 回答 2

5

readById正在返回一个所以,Promise您需要更改代码以使用它:

this.missionService.readById().then(missionData => console.log(missionData))
// or using async/await
this.missionData = await this.missionService.readById();
console.log(this.missionData);
于 2019-06-05T14:43:06.960 回答
0

其他问题,为什么这样:

  async onSubmit() {

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

用一个对象记录我的任务数据,然后很好。和这个

  onSubmit() {

    this.missionService.readById().then((mission) => {
      this.missionData = mission;
    });
    console.log(this.missionData);
   }

记录未定义?

于 2019-06-05T17:50:40.107 回答