Home > database >  userId dosen't get into the mongoose schema
userId dosen't get into the mongoose schema

Time:02-11

i'm trying to send a post request to create a new task for a user, the mongoose schema of each task is:

let todoSchema = new AppSchema ({
    userId: String,
    title: String,
    completed: Boolean
}, {versionKey: false})

and when i send the request, the task is created without the userId. this is the request:

add = () => {
        const task = {
            userId: this.props.id,
            title: this.state.title,
            completed: false,
        }

        if(task.title) {
            axiosUtils.create('http://localhost:8000/todos/', task)
            alert('task created!')
        }

    }

*the typeof task.userId is string, it is the mongoDB _id of the specific user toString()

CodePudding user response:

The function axios.create() just creates an axios instance with the specified config to send requests, but does not actually send any request. Here's the documentation about how to send a POST request with axios.

CodePudding user response:

You are doing axios.create() which is used to create an instance of axios. To know more about axios.create() you can refer the official axios doc: https://axios-http.com/docs/instance

In order to create a task you need to make a post request like:

axios.post("http://localhost:8080/todos/", { taskParam: task })

Here the taskParam is the name of that variable you are using to accept the task at backend in req.body most probably.

  • Related