我在我的项目中使用 netlify lambda 函数进行 CRUD 操作,但在 POST 操作中出现错误
我使用 angular front、netlify 函数和区区数据库
我的拉姆达:
/* code from functions/todos-create.js */
import faunadb from 'faunadb' /* Import faunaDB sdk */
/* configure faunaDB Client with our secret */
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
})
/* export our lambda function as named "handler" export */
exports.handler = (event, context, callback) => {
/* parse the string body into a useable JS object */
console.log('<<<<<<<<<' + " " + event.body + " " + '>>>>>>>>>')
const eventBody = JSON.stringify(event.body)
const data = JSON.parse(eventBody)
console.log("Function `todo-create` invoked", data)
const todoItem = {
data: data
}
/* construct the fauna query */
return client.query(q.Create(q.Ref("classes/todos"), todoItem))
.then((response) => {
console.log("success", response)
/* Success! return the response with statusCode 200 */
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log("error", error)
/* Error! return the error with statusCode 400 */
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}
我的一部分:
public myTodo = { title: 'What I had for breakfast ..',
completed: true };
onSubmit() {
this.missionService.createTodo(this.myTodo).then((response) => {
console.log('console log of missionService.createTodo ---->');
console.log(this.myTodo);
console.log('API response', response);
}).catch((error) => {
console.log('API error', error);
});
}
我的服务:
createTodo(data) {
console.log('console log of createTodo ---->');
console.log(data);
return fetch('/.netlify/functions/todos-create', {
body: JSON.stringify(data),
method: 'POST'
}).then(response => {
return response.json();
});
}
最后是执行错误....
Request from ::ffff:127.0.0.1: POST /todos-create
[BACK] [LAMBDA] <<<<<<<<< {"title":"What I had for breakfast ..","completed":true} >>>>>>>>>
[BACK] [LAMBDA] Function `todo-create` invoked {"title":"What I had for breakfast ..","completed":true}
[BACK] [LAMBDA] error { [BadRequest: validation failed]
[BACK] [LAMBDA] name: 'BadRequest',
[BACK] [LAMBDA] message: 'validation failed',
[BACK] [LAMBDA] requestResult:
[BACK] [LAMBDA] RequestResult {
[BACK] [LAMBDA] client:
[BACK] [LAMBDA] Client {
[BACK] [LAMBDA] _baseUrl: 'https://db.fauna.com:443',
[BACK] [LAMBDA] _timeout: 60000,
[BACK] [LAMBDA] _secret: '******************'
[BACK] [LAMBDA] _observer: null,
[BACK] [LAMBDA] _lastSeen: 1559144535321508 },
[BACK] [LAMBDA] method: 'POST',
[BACK] [LAMBDA] path: '',
[BACK] [LAMBDA] query: null,
[BACK] [LAMBDA] requestRaw: undefined,
[BACK] [LAMBDA] requestContent: Expr { raw: [Object] },
[BACK] [LAMBDA] responseRaw:
[BACK] [LAMBDA] '{"errors":[{"position":[],"code":"validation failed","description":"Instance data is not valid.","failures":[{"field":["data"],"code":"invalid type","description":"Invalid type String, expected type Map."}]}]}',
[BACK] [LAMBDA] responseContent: { errors: [Array] },
[BACK] [LAMBDA] statusCode: 400,
[BACK] [LAMBDA] responseHeaders:
[BACK] [LAMBDA] { 'content-type': 'application/json;charset=utf-8',
[BACK] [LAMBDA] date: 'Wed, 29 May 2019 15:42:15 GMT',
[BACK] [LAMBDA] 'x-bus-bytes-in': '0',
[BACK] [LAMBDA] 'x-bus-bytes-out': '0',
[BACK] [LAMBDA] 'x-bus-messages-in': '0',
[BACK] [LAMBDA] 'x-bus-messages-out': '0',
[BACK] [LAMBDA] 'x-faunadb-build': '2.6.4.rc4-3fa8865',
[BACK] [LAMBDA] 'x-faunadb-host': 'ec2-35-173-239-41.compute-1.amazonaws.com',
[BACK] [LAMBDA] 'x-points-network-out': '0.0',
[BACK] [LAMBDA] 'x-points-storage-read': '0.0',
[BACK] [LAMBDA] 'x-points-storage-write': '0.0',
[BACK] [LAMBDA] 'x-points-total': '0.0',
[BACK] [LAMBDA] 'x-query-bytes-in': '129',
[BACK] [LAMBDA] 'x-query-bytes-out': '209',
[BACK] [LAMBDA] 'x-query-time': '0',
[BACK] [LAMBDA] 'x-read-ops': '0',
[BACK] [LAMBDA] 'x-storage-bytes-read': '0',
[BACK] [LAMBDA] 'x-storage-bytes-write': '0',
[BACK] [LAMBDA] 'x-storage-ops-delete': '0',
[BACK] [LAMBDA] 'x-storage-ops-read': '0',
[BACK] [LAMBDA] 'x-storage-ops-write': '0',
[BACK] [LAMBDA] 'x-txn-delay': '0',
[BACK] [LAMBDA] 'x-txn-retries': '0',
[BACK] [LAMBDA] 'x-txn-time': '1559144535321508',
[BACK] [LAMBDA] 'x-write-ops': '0',
[BACK] [LAMBDA] 'content-length': '209',
[BACK] [LAMBDA] connection: 'Close' },
[BACK] [LAMBDA] startTime: 1559144535024,
[BACK] [LAMBDA] endTime: 1559144535439 } }
[BACK] [LAMBDA] Response with status 400 in 417 ms.
[BACK] [LAMBDA] (node:10193) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'statusCode' of undefined
[BACK] [LAMBDA] at callback (/home/mathieu/Bureau/acrabadabra/Acrabadabra/node_modules/netlify-lambda/lib/serve.js:35:42)
[BACK] [LAMBDA] at /home/mathieu/Bureau/acrabadabra/Acrabadabra/node_modules/netlify-lambda/lib/serve.js:67:7
[BACK] [LAMBDA] at tryCatch (/home/mathieu/Bureau/acrabadabra/Acrabadabra/functions/todos-create.js:1803:12)
[BACK] [LAMBDA] at invokeCallback (/home/mathieu/Bureau/acrabadabra/Acrabadabra/functions/todos-create.js:1818:13)
[BACK] [LAMBDA] at publish (/home/mathieu/Bureau/acrabadabra/Acrabadabra/functions/todos-create.js:1792:7)
[BACK] [LAMBDA] at flush (/home/mathieu/Bureau/acrabadabra/Acrabadabra/functions/todos-create.js:1522:5)
[BACK] [LAMBDA] at process._tickCallback (internal/process/next_tick.js:61:11)
[BACK] [LAMBDA] (node:10193) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
GET请求是有效的,我试过了。请问您对这个问题有任何想法吗?