I am trying to practice with building a website that customers can leave their contact information and I am trying to save what users (customers) typed into MongoDB but I need some help.
So this is my connect.js file:
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const connect = mongoose.connect("mongodb://127.0.0.1/test", {useNewUrlParser: true});
module.exports = { connect };
And this is my app.js file:
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
let port = 3000;
let app = express();
let publicPath = path.join(__dirname, "../public");
const { connect } = require("./db/connect");
const { UserRequest } = require("./models/user_request");
app.use(express.static(publicPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/user-requests", (req, res) => {
let userRequest = new UserRequest({
name: req.body.name,
email: req.body.email,
mobile: req.body.mobile,
experience: req.body.experience
});
connect.then((db) => {
db.collection("user-requests").insertOne(userRequest, (error, result) => {
if (error) {
return console.log(error);
}
res.send(result);
});
});
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
And my simple form:
<body>
<div class="col-lg-6">
<div class="card text-center card-form mt-4 mb-4">
<div class="card-body">
<h2>Contact Info</h2>
<form id="user_input" method="POST" action="/user-requests">
<div class="form-group">
<input type="text" name="apply-form-name" id="apply-form-name" class="form-control form-control-lg" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="apply-form-email" id="apply-form-email" class="form-control form-control-lg" placeholder="E-mail">
</div>
<div class="form-group">
<input type="tel" name="apply-form-tel" id="apply-form-tel" class="form-control form-control-lg" placeholder="Mobile Phone Number" maxlength="11">
</div>
<div class="form-group">
<input type="text" name="apply-form-exp" id="apply-form-exp" class="form-control form-control-lg" placeholder="Note">
</div>
<button type="submit" class="btn btn-success" id="submit-button">Submit</button>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" crossorigin="anonymous"></script>
</body>
So what I want to do is, save data that a user typed inside the form tags of HTML into MongoDB.
As you can see in the connect.js file which I tried to connect to the local MongoDB using Mongoose library, I exported MongoDB variable.
What I expected is user inputs are being saved as the Submit button is clicked on the website, but it returns an error saying:
(node:29016) UnhandledPromiseRejectionWarning: TypeError: db.collection is not a function
at connect.then (D:\OttoWebWorking\node.js\app.js:34:8)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:29016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:29016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process
with a non-zero exit code.
(node:29016) UnhandledPromiseRejectionWarning: TypeError: db.collection is not a function
at connect.then (D:\OttoWebWorking\node.js\app.js:34:8)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:29016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
I watched some tutorials online and thought it would be pretty simple but yeah. Can anyone help? Thanks in advance.