似乎无法在AWS Lambda / Nodejs中填充阵列

时间:2019-09-10 03:09:50

标签: node.js amazon-web-services aws-lambda

我正在使用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"
}

1 个答案:

答案 0 :(得分:0)

我猜测列表值和newString来自您的请求正文

const data = JSON.parse(event.body); // <-- This value is sent in a post request

例如,您可以发送带有以下内容的帖子请求:

{
    "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),
   });
}

希望这会有所帮助。