【问题标题】:Axios delete method with Lambda使用 Lambda 的 Axios 删除方法
【发布时间】:2022-01-04 05:50:46
【问题描述】:

我有以下代码从我的contactApp中删除一个联系人,它在React中使用axios来发送请求:

export const removeContact = createAsyncThunk(
  'contactsApp/contacts/removeContact',
  async (contactId, { dispatch, getState }) => {
    await axios.delete('https://api.com/prod', { 
      key1: `${contactId}`
     });
    console.log(">>>>>>>" + contactId)
    return contactId;
  }
);

这是从 DynamoDB 表中删除项目的 Lambda 函数:

var params = {
  TableName : 'Contacts',
  Key: {
    id: event.key1
  },
};

var documentClient = new AWS.DynamoDB.DocumentClient({region: "us-west-2"});

documentClient.delete(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});

问题是,该项目不会从表中删除,原因是 Lambda 没有收到来自 ReactApp 的“contactId”,其中包含应该删除的 id。因为当我像下面这样测试来自 Lambda 的代码时,它可以工作:

  Key: {
    id: "123" //Actual ID in table
  },

所以我认为问题在于反应:

key1: `${contactId}`

我错过了什么?

【问题讨论】:

    标签: javascript node.js reactjs lambda axios


    【解决方案1】:

    DELETE 请求没有正文。

    使用

    await axios.delete('https://api.com/prod', { 
      data: {key1: `${contactId}`}
     });
    

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 2018-03-05
      • 2018-10-06
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2018-10-17
      • 2020-03-08
      • 2018-12-24
      相关资源
      最近更新 更多