我正在尝试构建一个网站,客户可以保留其联系信息,并且试图保存用户(客户)在MongoDB中输入的内容,但是我需要一些帮助。
这是我的connect.js文件:
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const connect = mongoose.connect("mongodb://127.0.0.1/test", {useNewUrlParser: true});
module.exports = { connect };
这是我的app.js文件:
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}`);
});
还有我的简单表格:
<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>
所以我要做的是,将用户在HTML的表单标签中键入的数据保存到MongoDB中。
正如您在我尝试使用Mongoose库连接到本地MongoDB的connect.js文件中看到的那样,我导出了MongoDB变量。
在网站上单击“提交”按钮时,我期望的是用户输入被保存,但返回错误:
(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)
我在线观看了一些教程,并认为这很简单,但是。有人可以帮忙吗?预先感谢。