I need help making a code in which there are a couple text inputs in which you type a value and it stores it and it should be added to a json file. Here is my code:
index.pug:
meta(charset=“UTF-8”)
meta(http-equiv=“X-UA-Compatible”, content=“IE=edge”)
meta(name=“viewport”, content=“width=device-width,initial-scale=1”)
//- link(rel=“stylesheet”, href=“C:\Users\adity\OneDrive\Documents\Coding\web_dev\js_projects\Form\style.css”)
//- script(src=“C:\Users\adity\OneDrive\Documents\Coding\web_dev\js_projects\Form\app.js”)
style
include style.css
include app.js
title Document
ul
li
a(href="https://www.homecentre.in/in/en/") Home
li
a(href="https://www.hindustantimes.com/india-news") News
li
a(href="https://play.google.com/store/apps/details?id=com.google.android.contacts&hl=en_IN&gl=US") Contact
li
a(href="https://www.ienglishstatus.com/cool-whatsapp-status/") About
br
form(action="/form", method=“post”, class=“myForm”)
input(type=“text”, placeholder=“Enter your name”, name=“name”, id=“name”)
br
br
input(type=“number”, placeholder=“Enter your age”, name=“age”, id=“age”)
br
br
input(type=“password”, placeholder=“Enter your password”, name=“password”, id=“password”)
br
br
button(type=“submit”) submit
app.js
const express = require(“express”)
const path = require(“path”)
const app = express()
const port = 1470
const bodyparser = require(“body-parser”)
const mongoose = require(‘mongoose’);
mongoose.connect(‘mongodb://localhost/test’, {useNewUrlParser: true, useUnifiedTopology: true});
app.use(’/static’, express.static(‘static’)) // For serving static files
// Schema Mongoose
var formSchema = new mongoose.Schema({
name: String,
age: String,
password: String
});
// PUG SPECIFIC STUFF
app.set(‘view engine’, ‘pug’) // Set the template engine as pug
app.set(‘views’, path.join(__dirname, ‘views’)) // Set the views directory
// ENDPOINTS
app.get(’/’, (req, res)=>{
const params = {}
res.status(200).render('C:\\Users\\adity\\OneDrive\\Documents\\Coding\\web_dev\\js_projects\\Form\\index.pug', params);
})
app.post(’/form’, (req, res)=>{
var myData = "new Contact(req.body);"
myData.save().then(()=>{
res.send("This item has been saved to the database")
}).catch(()=>{
res.status(400).send("item was not saved to the databse")
})})
app.listen(port, ()=>{
console.log("Server is running on localhost 1470");
})
style.css:
body{
text-align: center;
background: tomato;
}
li{
list-style: none;
display: inline-block;
font-weight:bold;
/* background-color: white;
color: red; */
}
a{
color:wheat;
list-style: none;
text-decoration: none;
padding-left: 130px;
padding-right: 100px;
}
ul{
background-color: black;
}
My error is this: TypeError: myData.save is not a function
All help will be appreciated