【问题标题】:Product id already selected to delete but the product data is still on Firebase已选择要删除的产品 ID,但产品数据仍在 Firebase 上
【发布时间】:2020-09-09 23:03:49
【问题描述】:

我正在通过 redux-saga 实现删除产品功能,这是我的 deleteProduct 功能代码

import firebaseApp from "./config";

const firebaseDb = firebaseApp.database();

export const deleteProduct = (productId) => {


return firebaseDb
    .ref("products")
    .child(productId)
    .remove()
    .then(() => {
      console.log(`Deleted ${productId}`)
      return { status: "ok" }
    })
    .catch(() => ({ status: "error" }));
}

在我运行click to delete按钮后,它已经显示产品id已被删除,但产品数据并没有从数据库中删除。

这是我的reducer

const deleteProductRequest = (state, action) => ({
  ...state,
  loading: true,
  type: action.type,
});

const deleteProductSuccess = (state, action) => ({
  ...state,
  loading: false,
  product: action.data,
  type: action.type,
});

const deleteProductFailure = (state, action) => ({
  ...state,
  loading: false,
  error: action.error,
  type: action.type,
});

这是我的saga

export function* deleteProductRequest(action) {
  try {
    const { productId } = action;
    const response = yield call(deleteProduct, productId);

    if ((response.status === "ok")) {
      yield put(Creators.deleteProductSuccess(response.product));
    } else {
      yield put(Creators.deleteProductFailure(response.error));
    }
  } catch (error) {
    yield put(Creators.deleteProductFailure(error));
  }
}

我的容器是这样的

import { connect } from "react-redux";
import { Creators } from "../../actions/productAction";
import ProductTable from "../../pages/ProductTable";

const mapStateToProps = (state) => ({
  products: state.product.products,
  product: state.product.product,
  loading: state.product.loading,
  error: state.product.error,
});

const mapDispatchToProps = {
  ...Creators,
};

const PostNewContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(ProductTable);

export default PostNewContainer;

首先,我将所有产品列表从 Firebase 获取到一个表中,然后我开始执行产品操作,并逐步实现 API、reducer 和 Saga

任何人都了解此案,请您支持我。非常感谢

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database redux-saga


    【解决方案1】:

    firebaseDb.ref("products").child(productId)Reference,而 Reference 没有 removeValue() 方法。

    你应该使用remove()方法如下:

    return firebaseDb
      .ref("products")
      .child(productId)
      .remove();
    

    【讨论】:

    • 感谢您的评论。但是我的产品没有被删除。如何从 id 和产品数据中删除。我已经更新了我上面的问题。请你帮我检查一下吗
    • 另外,在更新你的问题时,请不要完全替换你最初的问题,因为以后的读者将不再理解相应的答案。请在初始问题内容后添加新内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2021-05-08
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    相关资源
    最近更新 更多