https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/todos/create.jsのサーバーレスコードを使用していますが、配列または追加を追加しようとすると私の関数への文字列は空の配列を返します(27行目)。
私はnodejsとawsに不慣れです助けてください
'use strict';
const uuid = require('uuid');
const AWS = require('aws-sdk'); // eslint-disable-line import/no-
extraneous-dependencies
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.create = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
text: data.text,
list: [], //i am unsure how to populate this with json
newString: "", //along with this string
createdAt: timestamp,
updatedAt: timestamp,
},
};
// write the todo to the database
dynamoDb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(params.Item),
};
callback(null, response);
});
};
このjsonで郵便配達員に投稿するとき。配列は空の文字列を返します。いつ人口を増やしたいか
{
"text": "hello",
"list": [1,3,4],
"newString": "hello2"
}
私はリスト値を推測しています、そしてnewStringはあなたのリクエスト本文から来ています
const data = JSON.parse(event.body); // <-- This value is sent in a post request
たとえば、次の本文を含むPOSTリクエストを送信できます。
{
"list": ['ABC', "DEF", "IJK"],
"newString": "Hello",
"text": "hello",
}
したがって、新しいコードは次のようになります。
'use strict';
const uuid = require('uuid');
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.create = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
text: data.text,
list: data.list, // This is now the value from your post request.
newString: data.newString, // Same as above.
createdAt: timestamp,
updatedAt: timestamp,
},
};
// write the todo to the database
dynamoDb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
// I have also moved the callback inside the put request as you should only return the data once you are sure that the data has been added to the DB.
callback(null, {
statusCode: 200,
body: JSON.stringify(params.Item),
});
}
お役に立てれば。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加