OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

2 PUT api request in same file in NEXT js

  • Thread starter Thread starter Charvi
  • Start date Start date
C

Charvi

Guest
I need advice how to solve when I need for one route 2x PUT request. I need to add and remove two kinds of inputs, but I can remove one or the other. I can't figure out a way to set route.js to be able to add and remove both kinds of inputs.

Code:
export async function PUT(request) {
const id = request.nextUrl.searchParams.get("id");
const result = await CargoDetails.findOneAndUpdate(
{
   $and: [{ _id: id }],
 },
 {
   $push: { defaultValues: { count: "", long: "", width: "", 
 weight: "", high: "" },
   },
 },{ new: true }
);
return NextResponse.json({ result }, { status: 200 });
}

Code:
 export async function PUT(request) {
 const id = request.nextUrl.searchParams.get("id");
 const result = await CargoDetails.findOneAndUpdate(
   {
     $and: [{ _id: id }],
   },
   {
     $push: {
       exwork: { cost: "" },
     },
   },
   { new: true }
 );

 return NextResponse.json({ result }, { status: 200 });
 }

Below you can see my Model for better idea:

Code:
const cargoDetailsSchema = new Schema(
  {
    defaultValues: { type: [formFields], default: undefined },
    exwork: { type: [exValue], default: undefined },
    client: String,
    sdl: String,
    price: Number,
    transport: String,
    menaFrom: String,
    menaTo: String,
  },

  { timestamps: true }
);

const CargoDetails =
  models?.CargoDetails || 
model("CargoDetails",cargoDetailsSchema);

export default CargoDetails;

defaultValues and exwork have their own plus and minus button for add and remove function. I know that that maybe is it not possible to do it this way. I'd be grateful for any advice. .

<p>I need advice how to solve when I need for one route 2x PUT request. I need to add and remove two kinds of inputs, but I can remove one or the other. I can't figure out a way to set route.js to be able to add and remove both kinds of inputs.</p>
<pre><code>export async function PUT(request) {
const id = request.nextUrl.searchParams.get("id");
const result = await CargoDetails.findOneAndUpdate(
{
$and: [{ _id: id }],
},
{
$push: { defaultValues: { count: "", long: "", width: "",
weight: "", high: "" },
},
},{ new: true }
);
return NextResponse.json({ result }, { status: 200 });
}
</code></pre>

<pre><code> export async function PUT(request) {
const id = request.nextUrl.searchParams.get("id");
const result = await CargoDetails.findOneAndUpdate(
{
$and: [{ _id: id }],
},
{
$push: {
exwork: { cost: "" },
},
},
{ new: true }
);

return NextResponse.json({ result }, { status: 200 });
}
</code></pre>
<p>Below you can see my Model for better idea:</p>
<pre><code>const cargoDetailsSchema = new Schema(
{
defaultValues: { type: [formFields], default: undefined },
exwork: { type: [exValue], default: undefined },
client: String,
sdl: String,
price: Number,
transport: String,
menaFrom: String,
menaTo: String,
},

{ timestamps: true }
);

const CargoDetails =
models?.CargoDetails ||
model("CargoDetails",cargoDetailsSchema);

export default CargoDetails;
</code></pre>
<p><code>defaultValues</code> and <code>exwork</code> have their own plus and minus button for add and remove function.
I know that that maybe is it not possible to do it this way. I'd be grateful for any advice. .</p>
 

Latest posts

Top