I am experiencing an issue with the PUT method on my API as it is not correctly setting the req.body data

Below is the code snippet for implementing the PUT method:

[/api/[id].ts]

case "PUT":
      try {
        const user = await UserModel.findOneAndUpdate(
          {
            _id: id,
          },
          {
            $set: req.body,
          }
        );

        if (!user) {
          return res.status(400).json({ success: false });
        }

        res.status(200).json(user);
      } catch (error) {
        res.status(400).json({ success: false });
      }
      break;

https://i.stack.imgur.com/GpG2u.png

The code does not update the money field as expected. However, changing the email field seems to work just fine.

Here is the User model used in this implementation:

[models/UserModel.ts]

import { model, models, Schema, SchemaTypes } from "mongoose";

const UserSchema = new Schema({
  name: {
    type: SchemaTypes.String,
    required: true,
  },
  email: {
    type: SchemaTypes.Number,
    required: false,
  },
  image: {
    type: SchemaTypes.String,
    required: true,
  },
  emailVerified: {
    type: SchemaTypes.Number,
    required: false,
  },
  money: {
    type: SchemaTypes.Number,
    required: true,
    default: 100,
  },
  luck: {
    type: SchemaTypes.Number,
    required: true,
    default: 1,
  },
});

export default models.User || model("User", UserSchema);

Answer №1

It's not a permanent solution, but I've found a creative workaround. Instead of utilizing the typical emailVerified and email fields, I repurposed them as luck and money, strangely enough it actually solved my problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Next.js with Express Application Deployment to Elastic Beanstalk: Troubleshooting Error 502

I am encountering persistent 502 Bad Gateway errors while attempting to deploy a next.js application using express to Electric Beanstalk. 2020/03/02 15:26:28 [error] 8286#0: *172 connect() failed (111: Connection refused) while connecting to upstream, ...

Asynchronous waiting waits not for async await

I'm currently working on a function that loops through an array and updates the model for each ID, then adds the result to another array. This is the code snippet I have: async function getSortedAnimals() { var i = 0; var sortedAnimals = []; id ...

Determining the return type of a function by analyzing its parameters

Let's consider the following scenario: export function bar(bar?: string) { return bar ? { bar } : {}; } const B1 = bar(); const B2 = bar("z"); Upon compilation, the types inferred for both B1 and B2 are: { bar: string; } | { bar? ...

What is the process for implementing a decorator pattern using typescript?

I'm on a quest to dynamically create instances of various classes without the need to explicitly define each one. My ultimate goal is to implement the decorator pattern, but I've hit a roadblock in TypeScript due to compilation limitations. Desp ...

Launch a Next.js application on Vercel and connect a subdomain through Cloudflare

I'm facing an issue when trying to deploy a Next.js app on Vercel and using Cloudflare for DNS management. My goal is to point dev.domain.com to the Vercel deployment. I added the subdomain in Vercel by going to Project Settings -> Domains. In Clou ...

The reason for the Jest failure is that it was unable to locate the text of the button

As someone who is new to writing tests, I am attempting to verify that the menu opens up when clicked. The options within the menu consist of buttons labeled "Edit" and "Delete". However, the test fails with the message: "Unable to find an element with te ...

Using FormData in conjunction with a NextJS API

I am in the process of developing a basic CRUD application using NextJS in combination with react-redux. The main function of this application is to store contact information for users. When attempting to add a new contact, I encounter an issue with sendin ...

Deployment of Next.js application with React Big Calendar on AWS CodeBuild encountered an error

While trying to deploy my project on AWS CodeBuild, I encountered an error message related to the react-big-calendar library. However, I am uncertain about what caused this issue. My setup includes Nextjs 13.4.2 and Node 18.6.0. Can anyone assist me in tro ...

Required to reload the page once a Facebook user gives permissions to proceed

I recently encountered an issue where I had to seek permission from a Facebook user in order to access specific information such as their email and birthday. Upon the initial launch of the application, if the user grants permission, they are then required ...

Passing the value of an Angular component to a different component

I have a menu in my application that uses IDs to route content, and I also have a detailed view where the content should be displayed based on those same IDs. Currently, I am trying to display objects by their ID when a button is clicked. However, I' ...

Retrieve the value of a hidden input when a button is clicked using reactive forms in Angular

I am currently attempting to retrieve the values of hidden input fields that are dynamically added when the user clicks on the "insert more" button. If you'd like to view the code in action, you can visit this StackBlitz link: get hidden input value ...

The puzzling error message in Next.js: "SyntaxError: Cannot use import statement outside a module"

Unique Situation While collaborating on a group project, one of the project maintainers suggested using Next.js. We are incorporating three.js into the project, and when using the GLTFLoader, I encountered an unexpected issue. Error: Import statement cann ...

Troubleshooting error messages with Angular 2 HttpClient response payload

Currently, I am implementing the latest version (4.3) of HttpClient in angular to handle data POST requests to my backend server: this.httpClient.post<View>(`/path`, data).subscribe( (view: View) => console.log("Success"), (error: HttpErrorRe ...

Encountering an Issue when Registering New Users in Database using Next.js, Prisma, and Heroku

Currently, I am immersed in my inaugural full-stack app development project, which is aligning with an online course. Unfortunately, I have encountered a major stumbling block that has persisted despite hours of troubleshooting. The issue arises when I try ...

Issue with Angular: ngForm object does not capture selected option

Revise to clean up unnecessary code. Having trouble displaying the selected option when I print the form object to the console. It's showing as undefined. Any guidance on what might be wrong with this code would be appreciated. Let me know if more in ...

Struggling to run npm build in Next.js project due to webpack errors?

After months of developing this application, I encountered a frustrating error when attempting to run npm run build for the first time today. Despite removing next-transpile-modules from my next.config.js and taking various troubleshooting steps like delet ...

Strategies for securing Firebase storage in Next.js without relying on Firebase authentication

Utilizing firebase storage for file uploads on my next.js page. Users complete a form and the files are uploaded to firebase storage (using a different database). I am seeking a way to restrict file uploads only within this post request. import React, { u ...

Angular 2: Enhancing User Experience with Pop-up Dialogs

Looking to implement a popup dialog that requests user input and returns the value. The popup component is included in the root component, positioned above the app's router outlet. Within the popup component, there is an open() method that toggles a ...

In search of a suitable approach for handling errors generically in Angular, using a JSON response received from Rails

Currently in the process of developing an application that is structured as a Rails server app providing RESTful APIs to the client. The Rails server utilizes RABL, while the client consists of an Angular JS application making standard $http calls (such as ...

Issue with Image Uploads (NodeJs/Formidable)

I tried using [email protected] to upload a form that includes an image, following a tutorial. However, I encountered the following error. How can I troubleshoot this issue? Error: The "path" argument must be of type string or an instance ...