myData.save is not a function

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 :pray:

In general I suggest (when debugging) that one reduce the problem to as few lines as possible that can still reproduce the problem. It reduces the number of places that you have to look.

What immediately jumps out at me are the following lines.

var myData = “new Contact(req.body);”
myData.save().then(()=>{

myData is a string at the point you attempt to call save(). Plus “never” use var in JS if it is avoidable (and it should be avoidable in all cases). This isn’t the cause of your error but it can lead to unexpected behavior. Look up “let” and “var hoisting” on a JS doc site.

I tried reducing but doesn’t happen. I will try removing vars though

@tleylan it does not help to remove the vars. The error still remains when i click the submit button. Why not you copy the code solve it and post it here. It would be better for me.

My mistake in explaining things better. You have assigned a string to myData there is no save method on that string. You probably need to remove the quotes from the assignment.

The use of var had nothing to do with it. The use of var doesn’t “break things” it used to be the only variable declaration you had. That is no longer the case. Read up on the “let” keyword to understand why you might want to avoid var.

Actually i had removed the quotes earlier and it doesn’t help. So i thought of putting the quotes

@tleylan plz send the solved code

I don’t have the solved code. I don’t know what your code does and it really isn’t important actually. The JS interpreter told you that myData.save is not a function so I believe it.

I do not know what a Contact is (that is part of your code not JS) but importantly one doesn’t just add or remove quotation marks to see what happens. The quotes are defining myData as a string. Not sometimes but always. So that may not be the only problem but it is at a minimum a contributor to the problem.

My suggestion is that you add some logging around that area have it console.log(myData) and it should report it as a string. If you can start a debugger then have it pause on the assignment of myData and inspect it.

I don’t know that the Contact constructor is expecting a request body as a parameter so perhaps that is the issue. Again, my suggestion is that you “reduce the area” that could contribute to the problem. All the other code is getting in your way. You can call that code from your server for testing purposes.

I can’t be of any more help.

Yes but isnt the point to give the code or explain what is wrong and to make it what?

I pointed out that you defined a string and tried to call a function on it didn’t I?

Assuming that you didn’t write the Contact function I will suggest that you check with the site where you got that. You have copied something incorrectly and if you compare the original to your version you can solve it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.