response 500 error when using axios post method express in vue.js

Viewed 1286

i confront by this problem that return Http 500 status code internal server error when use axios.post i'm using vue.js as frontend express as backend and axios for send request

look at my code:

backend: server.js

router.post('/users', async(req, res) =>{
    try{
        let user= new User();
        user.name = req.body.name;
        user.password = req.body.password;


        await user.save();

        res.json({
            status: true,
            message: "saved"
        });
    }
    catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});

frontend : user.vue

<template>
     <div>
      <v-row>
          <v-col cols="12" sm="12" md="6">
            <v-text-field
              type="text"
              shaped
              flat
              dark
              outlined
              v-model="name"
            ></v-text-field>
          </v-col>
          <v-col cols="12" sm="12" md="6">
            <v-text-field
              type="text"
              shaped
              flat
              dark
              outlined
              v-model="password"
            ></v-text-field>
          </v-col>
        </v-row>
        <br />
        <v-row align="center">
          <v-col class="col-7 text-left align-center">
            <div>
              <v-btn
                class="white--text col-3"
                color="green"
                style="text-decoration: none; color: inherit"
                @click="addUser"
              >
                <div>submit</div>
              </v-btn>
            </div>
          </v-col>
        </v-row>
      <div>
   </template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      name: "",
      password: "",
    };
  },
  methods: {
    async addUser() {
      try {
        const newUser = {
          name: this.name,
          password: this.password,
         
        };
        console.log(newUser);
        const response = await axios.post("http://localhost:3000/users", newUsers);
        console.log(response.data);
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>

in console.log(newUser) return to console correctly what i entered but the error exactly is

 POST http://localhost:3000/users 500 (Internal Server Error) xhr.js?b50d:177

 
Uncaught (in promise) Error: Request failed with status code 500  createError.js?2d83:16 
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:17)
    at XMLHttpRequest.handleLoad

when is use postman and post it works and the input values save in Database(mongodb)

0 Answers
Related