【问题标题】:Handle duplicate value error in CRUD app (react-redux + express-mongoose)处理 CRUD 应用程序中的重复值错误(react-redux + express-mongoose)
【发布时间】:2018-10-10 00:20:39
【问题描述】:

拥有mongoose 架构,其中carNumber 应该是唯一的:

var Schema = mongoose.Schema({
  createdAt: {
    type: Date,
    default: Date.now
  },
  carNumber: {
    type: String, index: {unique: true, dropDups: true},
  },
  carOwner: String
});

express控制器功能数据保存到db:

export const addCar = (req, res) => {
  const newCar = new Car(req.body);
   newCar.save((err, car) => {
    if (err) {
      return res.json({ 'success': false, 'message': 'Some Error' });
    }
     return res.json({ 'success': true, 'message': 'Car '+ car.carNumber +' added successfully', car });   
  }) 
}

但在尝试添加重复值时返回Unhandled Rejection (TypeError): Cannot read property 'carNumber' of undefined。为避免错误函数更新为验证undefined值:

export const addCar = (req, res) => {
  const newCar = new Car(req.body);
   newCar.save((err, car) => {
     if (car.carNumber === undefined) {
           return res.json({ 'success': false, '': 'Some Error' });
      }
    else {
        return res.json({ 'success': true, 'message': 'Car '+ car.carNumber +' added successfully', car });   
       }  
  }) 
}

但是在前端 redux 操作中获得 Unhandled Rejection (SyntaxError): Unexpected token P in JSON at position 0 response.json().then(error => { ... }

export const addNewCar = (car) => {
  console.log(car)
  return (dispatch) => {
    dispatch(addNewCarRequest(car));
    return fetch(apiUrl, {
      method: 'post',
      body: car,
    }).then(response => {
      if (response.ok) {
        response.json().then(data => {
          console.log(data.car);
          dispatch(addNewCarRequestSuccess(data.car, data.message))
        })
      }
      else {
        response.json().then(error => {
          dispatch(addNewCarRequestFailed(error))
        })
      }
    })
  }
}

感觉完全迷失在那里......可能有人陷入同样的​​问题吗?

【问题讨论】:

  • 我认为您将.then() 链接到您的json() 而不是您的fetch()
  • @Colin 我应该使用catch 吗?
  • 是的,您需要将.then() 放在正确的位置,然后.catch() 才能发现任何错误。
  • @Colin 你的意思是“正确”的地方?
  • 我不明白。什么?

标签: mongodb reactjs express mongoose react-redux


【解决方案1】:

正如 cmets 中所述,我认为您的 json() 上有 .then() 而不是 fetch()。你需要一些类似的东西:

export const addNewCar = car => {
  console.log(car);
  return dispatch => {
    dispatch(addNewCarRequest(car));
    return fetch(apiUrl, {
      method: "post",
      body: car
    })
      .then(response => {
        if (response.ok) {
          return response.json();
        } else {
          throw Error('blah')
        }
      })
      .then(data => {
        console.log(data.car);
        dispatch(addNewCarRequestSuccess(data.car, data.message));
      })
      .catch(error => {
        dispatch(addNewCarRequestFailed(error));        
      });
  };
};

【讨论】:

  • 非常感谢帮助,毕竟它带来了错误:i.gyazo.com/bb9aff25fddac33955dcd150a147f5af.png ,想了解如何解决它,你能详细说明一下吗?
  • 似乎 .catch 将 undefined 值发送到 cars 数组,现在它正在工作,但似乎是解决方法。
猜你喜欢
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 2021-08-29
  • 2023-03-04
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
相关资源
最近更新 更多