猫鼬:TypeError:'mongooseSchemahere'不是一个函数

时间:2019-11-25 16:36:19

标签: javascript node.js mongodb express mongoose

在models / user.js中,我有以下猫鼬模式设置:

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({

    loginId: String,
    firstname: String,
    lastname: String,
    eMail: String,
    password: String,
    active: Boolean

});

module.exports = userSchema; 

在我的主要app.js中,我有以下代码:

const mongoose = require('mongoose');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url,  {
  useUnifiedTopology: true,
  useNewUrlParser: true,
  },function(err, db) {
  if (err) throw err;
  var dbo = db.db("db");
  dbo.collection("db").find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
}); 

let userSchema = require('./models/user.js');
// Get single user
app.get('/user/:id', function (req, res) {
  userSchema.findById(req.params.id, (error, data) => {
    if (error) {
      return next(error)
    } else {
      res.json(data)
    }
  })
})

我得到标题中的错误(只需用userSchema替换mongooseSchemahere)。我做错了什么?我尝试将userSchema声明放在不同的位置,但没有帮助。

2 个答案:

答案 0 :(得分:1)

您需要导出模型而不是模式。

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    loginId: String,
    firstname: String,
    lastname: String,
    eMail: String,
    password: String,
    active: Boolean

});

module.exports =  mongoose.model('user', userSchema);

现在您可以执行以下操作:

let User = require('./models/user.js');
// Get single user
app.get('/user/:id', function (req, res) {
  User.findById(req.params.id, (error, data) => {
    if (error) {
      return next(error)
    } else {
      res.json(data)
    }
  })
})

答案 1 :(得分:1)

您需要使用mongoose.connect来处理猫鼬模型。

进行以下更改:

1-)像这样创建用户模型并导出:

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
  loginId: String,
  firstname: String,
  lastname: String,
  eMail: String,
  password: String,
  active: Boolean
});

module.exports = mongoose.model("User", userSchema);

2-)更改您的App.js,以将数据库与mongoose.connect连接起来:

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const User = require("./models/user");
const url = "mongodb://localhost:27017/mydb";

const port = process.env.PORT || 3000;

app.use(express.json());

mongoose
  .connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    app.listen(port, () => {
      console.log(`App running on port ${port}...`);
    });
  })
  .catch(error => console.log(error));

现在您可以创建一个这样的用户:

app.post("/user", function(req, res, next) {
  console.log("Req body:", req.body);
  User.create(req.body)
    .then(result => {
      console.log({ result });
      res.send(result);
    })
    .catch(err => {
      console.log(err);
      res.status(500).send("something went wrong");
    });
});

要通过_id检索用户,

app.get("/user/:id", function(req, res, next) {
  User.findById(req.params.id, (error, data) => {
    if (error) {
      return next(error);
    } else {
      res.json(data);
    }
  });
});

要按名字检索用户:(如果要按名字查找所有用户,请更改find​​One进行查找。):

app.get("/user/firstname/:firstname", function(req, res, next) {
  console.log(req.params.firstname);
  User.findOne({ firstname: req.params.firstname }, (error, data) => {
    if (error) {
      return next(error);
    } else {
      res.json(data);
    }
  });
});