axios 如何删除除一条记录外的所有记录?

cld4siwp  于 2023-03-29  发布在  iOS
关注(0)|答案(1)|浏览(136)

我用getIds函数调用所有id,并在clearSales函数中使用,clearSales函数在测试结束时运行,当我调用clearSales时,所有记录都被删除了,我不想删除所有记录,只保留一条记录,如何按照下面的函数删除除一条记录外的所有记录?

const getIds= async () => {
    let res = await axios({
        method: 'get',
        url: '/v1/sales'
    })
    expect(res.status).toBe(200)
    const ids = [];
    res.data.salesId.forEach(item => {
        ids.push(item.id);
    });
    return ids
};

export const clearSales = async () => {
    const idList = await getIds()
    let res = await axios({
        method: 'post',
        url: '/v1/feed/bulk_update',
        data: { "feed_ids": idList, "operation": "delete" },
    })
    expect(res.status).toBe(200)
};
jvlzgdj9

jvlzgdj91#

只需从idList中删除您想要保留的记录的ID。这里是一个实现示例:

export const clearAllOtherSales = async (remainingRecordId) => {
    let idList = await getIds();
    let index = idList.findIndex(id => id === remainingRecordId);
    if (index > -1) idList.splice(index, 1);
    let res = await axios({
        method: 'post',
        url: '/v1/feed/bulk_update',
        data: { "feed_ids": idList, "operation": "delete" },
    });
    expect(res.status).toBe(200);
}

clearAllOtherSales(115)清除除id为115的销售外的所有销售。

相关问题