English 中文(简体)
不得阅读未界定(翻新面积)的特性——次之十三
原标题:Cannot read properties of undefined (reading size ) - Next js 13

今天,我提出一个问题,即使用13.4版本的“后继器”。 (一) 使用搜查 在服务器方面,它正 throw倒: 不能读到未经界定的(接近大小)iam结构大小和风格搜索束的特性。 这是我的法典:

Iam performing add to cart functionlaity, which speaks to backend. this is my addToCart function:

 const addToCartHandler = async () => {
    const style = Number(searchParams.get("style"));
    const size = searchParams.get("size");
    console.log("iam from front", style, Number(size));
    await axios
      .get(`/api/product/${product._id}?style=${style}&size=${size}`)
      .then((res) => console.log("data------>", res.data));       
  };

这是我的产物/[id]/价值:

import { NextResponse } from "next/server";
import connect from "../../../../utils/db";
export const GET = async (
  req: Request,
  { params, searchParams }: { params: any; searchParams: any }
) => {
  try {
    await connect();
    const { id } = params;
    const size = searchParams.size;
    const style = searchParams.style;
    console.log(id, size, style);
    return new NextResponse(id, { status: 200 });
  } catch (error: any) {
    return new NextResponse(`${error}`, {
      status: 500,
      statusText: error.message,
    });
  }
};

I have tried this above code after clicking add to cart button, it throws me enter image description here

I think there s an error in backend. Please ignore it if you think this is stupid, it s a beginner mistake. :)

问题回答

在NodeJS的世界上,搜查Params是一种有多种方法的物体,直接回报价值。 在此情况下,searchrams.size>将eq an instance of URLSearchParams Object,意义的是searchParams equal to a URLSearchrams.size.size

正确的确定方式是

    const size = searchParams.get( size );
    const style = searchParams.get( style );

See official NodeJS doc for searchParams here https://nodejs.org/api/url.html#urlsearchparams





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签