Mongodb Mongoose数据未保存到数据库
我试图使用mongoose/express将表单数据添加到数据库中。该集合是在数据库中创建的,虽然提交时没有错误,并且my console.log确认数据与生成的ID一起存在,但我在数据库中仍然没有任何条目,您能看到我缺少的内容吗?任何帮助都将不胜感激Mongodb Mongoose数据未保存到数据库,mongodb,express,mongoose,mongoose-schema,Mongodb,Express,Mongoose,Mongoose Schema,我试图使用mongoose/express将表单数据添加到数据库中。该集合是在数据库中创建的,虽然提交时没有错误,并且my console.log确认数据与生成的ID一起存在,但我在数据库中仍然没有任何条目,您能看到我缺少的内容吗?任何帮助都将不胜感激 **CONFIG** const port = process.env.PORT || 3000, bodyparser = require("body-parser"), mongoos
**CONFIG**
const port = process.env.PORT || 3000,
bodyparser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
app.set("view engine", "ejs");
app.use(
bodyparser.urlencoded({
extended: true
})
);
app.use(
require("express-session")({
secret: "Mama Mia Thats a Spicy Meatball!",
resave: false,
saveUninitialized: false
})
);
app.use(express.static(__dirname + "/styles/"));
app.use(express.static(__dirname + "/Media/"));
app.use(express.static(__dirname + "/JS/"));
mongoose.connect("mongodb://localhost/lashB", {
useNewUrlParser: true
});
const mongCon = mongoose.connection;
mongCon.on("connected", function () {
console.log(`beep boop
database mainframe syncroniiiiiiized`);
});
mongCon.on("disconnected", function () {
console.log(`You're off the database now...Daniel San.`);
});
mongCon.on('error', function () {
console.log(error, `We've got company...there's an error!!!`);
});
**SCHEMA**
var customerSchema = new mongoose.Schema({
firstName: String,
lastName: String,
mobile: String,
email: String,
treatment: String,
time: String,
date: Date
});
var Customer = mongoose.model("Customer", customerSchema, "Customer");
**ROUTES**
app.get("/", function (req, res) {
res.render("main");
});
app.get("/appointment", function (req, res) {
res.render("appointment");
});
app.get("/appointmentConfirmation", function (req, res) {
res.render("appConfirmation");
});
app.get("/blogHome", function (req, res) {
res.render("blogHome");
});
app.post('/appointment', function (req, res) {
var firstName = req.body.firstName,
lastName = req.body.lastName,
mobile = req.body.mobile,
email = req.body.email,
treatment = req.body.treatment,
time = req.body.time,
date = req.body.date;
var deetz = {
date: date,
time: time,
treatment: treatment,
email: email,
mobile: mobile,
firstName: firstName,
lastName: lastName
}
Customer.create(deetz, function (err, info) {
if (err) {
console.log(err, `something went wrong`);
} else {
console.log(info);
console.log('newly created file for a customer');
}
});
res.render('appConfirmation', {
firstName: firstName
}
);
});
app.listen(port, function () {
console.log(`You rockin' now on port ${port}`);
});
修好了。错误在于,我输入了集合的“use”名称,但“use”用于db而不是集合。所以show dbs>use nameofdb>db.nameofcollection.find()工作得很好。看起来不错,您可能需要重新加载数据库以查看新数据results@AnkitManchanda我重新加载到db,但没有乐趣,我甚至尝试使用insertOne方法,我的控制台确认数据已经发送并分配了一个id,但是当我检查mongoose时,我什么都没有,所以如果我使用Compass GUI,数据是存在的,但是在shell中,绝对没有,有人有什么想法吗?我正在键入show dbs>use nameofdb>show collections>use nameofcollection>db.nameofcollection.find,但什么都没有