修改数据

Endpoints可以定义获取数据的方法,也可以定义 HTTP 动词来修改数据。 它们是GETPOSTPUTPATCHDELETE。我们已经在上一节讨论了 GET 动词。 这些动词中的每一个都有相应的 on___ 方法,例如 onGetonPostonPutonPatchonDelete

// File: src/routes/product/[skuId]/index.tsx
import type { RequestHandler } from '@builder.io/qwik-city';

type EndpointData = ProductData | null;

interface ProductData {
  skuId: string;
  price: number;
  description: string;
}
export const onPut: RequestHandler<EndpointData> = async ({ url, params, request, response }) => {
  // put your DB access here (hard coding a response for the simplicity)

  // read data from request and perform DB update.
  // console.log(params.skuId);
  // console.log(request.method);
  // console.log(url.pathname);

  // set response headers
  response.headers.append('Cache-Control', ' public, max-age=86400');

  // return data to be access from `useEndpoint()`
  return {
    skuId: params.skuId,
    price: 123.45,
    description: `Description for ${params.skuId}`,
  };
};
Made with ❤️ by