来自nodejs应用程序mongose.save()块,什么都不做?

问题描述 投票:0回答:2

我正在尝试使用一种非常基本的MongoDB方法是使用Mongoose将文档保存到数据库中。

1。我在centos 7中安装了MongoDB

2。使用以下方法在Mongo Shell上创建数据库:使用mydatabase并像往常一样在其中插入文档。

3。安装mongoose并在我的nodejs应用程序和MongoDB之间建立连接:

mongoose.connect('mongodb://localhost:27017/goClass_test', {
  useUnifiedTopology: true,
  useNewUrlParser: true,
});

4。测试连接,一切都很好:

db.once('open', () => {
  console.log('DB Connected');
});
  1. 正常导入模型模式:

var { Classe } = require('../DBModels/GoClassDBModels/classes');

  1. 尝试像这样添加新文档:
var newClasse = new Classe({
     label: 'hello',
     level: 'level',
   });
newClasse.save()

我的模特是:

const mongoose = require('mongoose');
const { Schema } = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;

var classSchema = new Schema({
  directeurId: {
    type: ObjectId,
  },
  label: {
    type: String,
  },
  level: {
    type: String,
  },
  studentNbr: {
    type: String,
  },
});

var Classe = mongoose.model('Classes', classSchema);

module.exports = { Classe };

SERVER.JS:

const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const cookieParser = require('cookie-parser');
const _ = require('lodash');

var app = express();
var server = http.createServer(app);
server.listen(80, () => {
  console.log('server is started on 80');
});

mongoose.connect('mongodb://localhost:27017/goClass_test', {
  useUnifiedTopology: true,
  useNewUrlParser: true,
});

console.log(mongoose.connection.host);
console.log(mongoose.connection.port);

let db = mongoose.connection;

db.once('open', () => {
  console.log('DB Connected');
});

db.on('error', (err) => {
  console.log(err);
});

var { Classe } = require('../DBModels/GoClassDBModels/classes');

const goClasseRouteDirecteur = require('./GOClassRoutes/DirecteurRoutes/subRoutesClass');

app.use(bodyParser.json());
app.use(cookieParser(['lakkini.com']));
app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);

app.use(function (req, res, next) {
  res.set(
    'Cache-Control',
    'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
  );
  next();
});

app.set('view engine', 'hbs');
app.use(express.static('/home/admin/SMS'));
app.use(express.static('/home/admin/assets'));

app.get('/', (req, res) => {
  res.render('SMS/dashboard.hbs');
});

app.get('/classes', (req, res) => {
  res.render('SMS/classes.hbs');
});

app.get('/inscription', (req, res) => {
  res.render('SMS/inscriptions.hbs');
});

app.post('/saveClasse', (req, res) => {
  var newClasse = new Classe({
    label: 'hello',
    level: 'level',
  });
  console.log('im gonna save the new class');
  newClasse.save((err, response) => {
    if (err) console.log(err);
    else console.log(response);
  });
});

问题是:什么也没发生。没有插入文档,也没有错误。你能给我建议吗?

PS:我正在尝试从没有HTTPS的HTTP服务器请求。

会影响我的请求并阻止保存到数据库吗?

结果:click to see the picture of the result please

node.js mongodb mongoose centos7 mongoose-schema
2个回答
0
投票

由于没有为猫鼬连接提供整个文件,并且在调用save函数的地方,假设您已经正确地构造了文件,​​所以我给出了答案。我能够这样做,

模式(与您的模式相同):

const mongoose = require("mongoose");
const { Schema } = require("mongoose");
var ObjectId = mongoose.Schema.Types.ObjectId;

var classSchema = new Schema({
  directeurId: {
    type: ObjectId,
  },
  label: {
    type: String,
  },
  level: {
    type: String,
  },
  studentNbr: {
    type: String,
  },
});

var Classe = mongoose.model("Classes", classSchema);

module.exports = { Classe };

插入功能:

mongoose.connect("mongodb://localhost:27017/goClass_test", {
    useUnifiedTopology: true,
    useNewUrlParser: true,
  });

  var db = mongoose.connection;
  db.on("error", console.error.bind(console, "connection error:"));

  db.once("open", () => {
    console.log("DB Connected");
    var newClasse = new Classe({
      label: "hello",
      level: "level",
    });

    newClasse.save();
  });

UPDATE:

SERVER.JS

const mongoose = require("mongoose");
const express = require("express");
const bodyParser = require("body-parser");
const http = require("http");
const cookieParser = require("cookie-parser");
const _ = require("lodash");

var app = express();
var server = http.createServer(app);
server.listen(80, () => {
  console.log("server is started on 80");
});

mongoose.connect("mongodb://localhost:27017/goClass_test", {
  useUnifiedTopology: true,
  useNewUrlParser: true,
});

console.log(mongoose.connection.host);
console.log(mongoose.connection.port);

let db = mongoose.connection;

db.once("open", () => {
  console.log("DB Connected");
});

db.on("error", (err) => {
  console.log(err);
});

var { Classe } = require("./models/Classe");

// const goClasseRouteDirecteur = require('./GOClassRoutes/DirecteurRoutes/subRoutesClass');

app.use(bodyParser.json());
app.use(cookieParser(["lakkini.com"]));
app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);

app.use(function (req, res, next) {
  res.set(
    "Cache-Control",
    "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0"
  );
  next();
});

app.set("view engine", "hbs");
// app.use(express.static("/home/admin/SMS"));
// app.use(express.static("/home/admin/assets"));

// app.get('/', (req, res) => {
//     res.render('SMS/dashboard.hbs');
//   });

//   app.get('/classes', (req, res) => {
//     res.render('SMS/classes.hbs');
//   });

//   app.get('/inscription', (req, res) => {
//     res.render('SMS/inscriptions.hbs');
//   });

app.post("/saveClasse", (req, res) => {
  var newClasse = new Classe({
    label: "hello",
    level: "level",
  });
  console.log("im gonna save the new class");

  newClasse.save((err, response) => {
    if (err) console.log(err);
    else console.log("RESPONSE" + response);
  });
});
enter image description here

0
投票

我找到了问题,但我不明白为什么。第一个结构是:

**

  • DBModel
    • classes.js
  • 服务器
    • server.js **

当前结构:

**

  • DBModel

    • classes.js
  • server.js

**

我从该文件夹中找出服务器,并且一切正常... ??

为什么?

热门问题
推荐问题
最新问题