如何使用 lambda 函数访问我的 Get 请求的内容
How can I access to the content of my Get request using lambda function
我需要获取存储在我的 faunaDB 数据库中的对象实例,但我无法在我的前面获取他的内容
我试图找出控制台日志的问题,但是...
这是我的 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)
})
})
}
我在 angular 服务中的功能:
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(), … }
我不明白如何得到我的对象,如果你能解释我...
非常感谢
readById
返回一个Promise
所以,你需要改变你的代码来使用这个:
this.missionService.readById().then(missionData => console.log(missionData))
// or using async/await
this.missionData = await this.missionService.readById();
console.log(this.missionData);
其他问题,为什么会这样:
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);
}
日志未定义?
我需要获取存储在我的 faunaDB 数据库中的对象实例,但我无法在我的前面获取他的内容
我试图找出控制台日志的问题,但是...
这是我的 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)
})
})
}
我在 angular 服务中的功能:
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(), … }
我不明白如何得到我的对象,如果你能解释我... 非常感谢
readById
返回一个Promise
所以,你需要改变你的代码来使用这个:
this.missionService.readById().then(missionData => console.log(missionData))
// or using async/await
this.missionData = await this.missionService.readById();
console.log(this.missionData);
其他问题,为什么会这样:
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);
}
日志未定义?