I've just recently started learning Vue.JS. I'm trying to make a simple MEVN to-do list app to understand how it works, but whenever I try to send a POST to my express server it just tells me:
TypeError: Cannot read properties of undefined (reading 'TodoItem')
Can anyone tell me what I'm doing wrong? When I console log the todo item before sending it, it logs whatever I'm sending.
Todo.vue
<template>
<div>
<form @submit.prevent="createPost">
<div>
<label for="todoItem">Todo Item</label>
<input type="text" id="todoItem" v-model="todoItem">
</div>
<button>Create Post</button>
</form>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: "Todo",
data() {
return {
todoItem: ''
}
},
methods: {
createPost() {
console.log("Sending: " + this.todoItem)
axios.post("http://[SERVER]/todo", this.todoItem)
.then((response)=> console.log(response))
.catch((error)=> console.log(error))
}
}
}
</script>
server.js
const express = require("express")
const bodyParser = require("body-parser")
const app = express()
app.set(bodyParser.json())
app.set(bodyParser.urlencoded({extended: true}))
app.get("/", (req, res)=> {
res.send("Hello, World!")
})
app.post("/todo", (req, res)=> {
const todoItem = req.body.todoItem
console.log(todoItem)
res.status(200)
res.json({
message: "success"
})
})
app.listen(3000, "0.0.0.0", ()=> {
console.log("Server is up and running.")
})
Edit: I followed Teddy's answer and now I'm getting this error from Vue.JS https://i.imgur.com/zq2W2AR.png