您了解这个带有 lambda 函数的 POST 请求的问题吗?
2019-05-29
1989
我在我的项目中使用 netlify lambda 函数进行 CRUD 操作,但在 POST 操作中出现错误
我使用 angular front、netlify 函数和 faunaDB
我的 lambda:
/* 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 请求可以运行,我试过了。 请问您对问题有什么想法吗?
2个回答
我的解决方案是在请求的参数变量中添加第二个 JSON 解析:
const todoItem = {
data: JSON.parse(data)
}
Math73100
2019-05-31
删除 stringify 不会改变任何东西,我在 lambda 函数中添加了控制台日志以获取更多详细信息:
/* 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)
console.log('<<<<<<<<<' + " " + eventBody + " " + '>>>>>>>>>')
const data = JSON.parse(eventBody)
console.log('<<<<<<<<<' + " " + data + " " + '>>>>>>>>>')
console.log("Function `todo-create` invoked", data)
const todoItem = {
data: data
}
// {"title":"What I had for breakfast ..","completed":true}
console.log('<<<<<<<<<' + " " + todoItem + " " + '>>>>>>>>>')
/* 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)
})
})
}
fetch body 中没有 stringify 时出错:
[BACK] [LAMBDA] Request from ::ffff:127.0.0.1: POST /todos-create
[BACK] [LAMBDA] <<<<<<<<< [object Object] >>>>>>>>>
[BACK] [LAMBDA] <<<<<<<<< "[object Object]" >>>>>>>>>
[BACK] [LAMBDA] <<<<<<<<< [object Object] >>>>>>>>>
[BACK] [LAMBDA] Function `todo-create` invoked [object Object]
[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: 1559289243066157 },
[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: 'Fri, 31 May 2019 07:54:03 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': '82',
[BACK] [LAMBDA] 'x-query-bytes-out': '209',
[BACK] [LAMBDA] 'x-query-time': '3',
[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': '1559289243066157',
[BACK] [LAMBDA] 'x-write-ops': '0',
[BACK] [LAMBDA] 'content-length': '209',
[BACK] [LAMBDA] connection: 'Close' },
[BACK] [LAMBDA] startTime: 1559289242318,
[BACK] [LAMBDA] endTime: 1559289243199 } }
[BACK] [LAMBDA] Response with status 400 in 883 ms.
[BACK] [LAMBDA] (node:5201) 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:5201) 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: 7)
错误:
[BACK] [LAMBDA] Request from ::ffff:127.0.0.1: POST /todos-create
[BACK] [LAMBDA] <<<<<<<<< {"title":"What I had for breakfast ..","completed":true} >>>>>>>>>
[BACK] [LAMBDA] <<<<<<<<< "{\"title\":\"What I had for breakfast ..\",\"completed\":true}" >>>>>>>>>
[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] <<<<<<<<< [object Object] >>>>>>>>>
[BACK] [LAMBDA] (node:11199) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[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: 1559294662744534 },
[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: 'Fri, 31 May 2019 09:24:22 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': '1559294662744534',
[BACK] [LAMBDA] 'x-write-ops': '0',
[BACK] [LAMBDA] 'content-length': '209',
[BACK] [LAMBDA] connection: 'Close' },
[BACK] [LAMBDA] startTime: 1559294662390,
[BACK] [LAMBDA] endTime: 1559294663102 } }
[BACK] [LAMBDA] Response with status 400 in 762 ms.
[BACK] [LAMBDA] (node:11199) 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:11199) 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: 1)
[BACK] [LAMBDA] (node:11199) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
它直接与 lambda 函数一起使用:
const todoItem = {
data: {"title":"What I had for breakfast ..","completed":true}
}
Math73100
2019-05-31