尝试使用它来更新创建文章时的另一个表时未定义offer.id

问题描述 投票:0回答:0

我想创建一个选项,用户可以在其中选择最喜欢的优惠。我正在使用棱镜和 nextjs。我有它,这样作者就可以创建一篇文章,当他创建时,最喜欢的表会找到 offer.id 并同时更新。嗯,就是这个主意。在 handleSubmit 函数中,作者创建了文章,我获取了所有文章并将它们存储在一个状态中,我找到了 id 并将其存储在一个新状态中,最后我使用 id 更新收藏夹。

如果我这样做,当作者创建文章时,他会得到未定义的 offer.id,但是如果他点击按钮两次,他会创建两篇文章,有 2 个不同的 id,只有一个匹配的收藏夹。

我要一篇文章,一个观看offer.id.

我尝试创建一个变量来定义 offer.id,然后再将它传递给创建收藏夹,但它没有解决问题。错误发生在收藏夹功能之前。这是代码截图(按钮只是

 onClick=.  {handleSubmit}
):

 `
     const getOffers = async () => {
     const response = await axios.get('/api/offers');
     const data = await response.data;
     setOffers(data);
    };



const handleSubmit = async (e) => {
e.preventDefault();
const userId = props.data.user.id;

const response = await axios.post(
  '/api/offers/create',
  {
    ...formState,
    userId,
  },
  {
    headers: {
      'Content-Type': 'application/json',
    },
  }
);
getOffers();
const offer = offers.find(
  (off) =>
    off.title === formState.title &&
    off.contractType === formState.contractType &&
    off.location === formState.location &&
    off.company === formState.company &&
    off.companyDescription === formState.companyDescription &&
    off.description === formState.description &&
    off.skills === formState.skills &&
    off.website === formState.website &&
    off.categoryId === formState.categoryId
);
setOffer(offer);
const offerIdentifier = offer.id;

const res = await axios.post('/api/favorite/create', {
  offerId: offerIdentifier,
  isFavorite: true,
});

router.push('/');

}; `

database api next.js undefined
© www.soinside.com 2019 - 2024. All rights reserved.