简体   繁体   English

MongoDB 参考错误:未定义架构

[英]MongoDB referenceError: Schema is not defined

I've read the questions in this topic.我已阅读本主题中的问题。 But I wasn't able to find the solution, because I think I defined the Schema well.但是我找不到解决方案,因为我认为我很好地定义了 Schema。

My models.js is like this.我的models.js是这样的。

var mongoose = require('mongoose');


var userSchema = new mongoose.Schema({
username: String,
password: String,
created_at: { type: Date, default: Date.now }
});

var todoSchema = new mongoose.Schema({
title: String,
done: Boolean,
priority: String,
deadLine: String,
masterDetailNote: String
});

mongoose.model('User', userSchema);
mongoose.model('Todo', todoSchema);

My api.js is like this.我的 api.js 是这样的。

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Todo = mongoose.model('Todo', todoSchema);

router.use(function(req, res, next) {

if (req.method === "GET") {
    return next();
}

if (!req.isAuthenticated()) {
    return res.redirect('#/login');
}
return next();
});
router.route('/posts')

//returns all todos
.get(function(req, res) {

Todo.find(function(err, data) {

    if (err) {
        return res.send(500, err);
    }

    return res.send(data);

 });

})

//creates a new todo
.post(function(req, res) {

var todo = new Todo();
todo.title = req.body.title;
todo.done = req.body.done;
todo.priority = req.body.priority;
todo.deadLine = req.body.deadLine;
todo.masterDetailNote = req.body.masterDetailNote;

todo.save(function(err, post) {
    if (err) {
        return res.send(500, err);
    }
    return res.json(todo);
  });
}); 

router.route('/posts/:id')

//egy bizonyos Todo-t hoz vissza
.get(function(req, res) {
Todo.findById(req.params.id, function(err, post) {
    if (err)
        res.send(err);
    res.json(post);
});
})

//frissít egy todot
.put(function(req, res) {
Todo.findById(req.params.id, function(err, post) {
    if (err)
        res.send(err);

    todo.title = req.body.title;
    todo.done = req.body.done;
    todo.priority = req.body.priority;
    todo.deadLine = req.body.deadLine;
    todo.masterDetailNote = req.body.masterDetailNote;

    todo.save(function(err, post) {
        if (err)
            res.send(err);

        res.json(post);
    });
   });
 })

.delete(function(req, res) {
Todo.remove({
    _id: req.params.id
}, function(err) {
    if (err)
        res.send(err);
    res.json("deleted :(");
});
});

module.exports = router;

When i try to strat the server it shows the error message.当我尝试对服务器进行分层时,它会显示错误消息。

var Todo = mongoose.model('Todo', todoSchema);

RefferenceError: todoSchema is not defined.参考错误:todoSchema 未定义。 I don't really understand because it is defined exactly like the userSchema.我不太明白,因为它的定义与 userSchema 完全一样。 But I can use the userSyhema without any error.但是我可以毫无错误地使用 userSyhema。

Because of todoSchema is not defined in app.js use without like: mongoose.model('modelName') 由于未在app.js定义todoSchema因此不使用以下代码: mongoose.model('modelName')

so in your api.js use like 所以在你的api.js中使用像

var Todo = mongoose.model('Todo');// ensure first model file is loaded 

instead of 代替

var Todo = mongoose.model('Todo', todoSchema);

OR 要么

require('modelDirectory/model.js');//load model file first then use model from there 
var Todo = mongoose.model('Todo');

OR 要么

create separate file for each model. 为每个模型创建单独的文件。 for example in todo.js 例如在todo.js

var mongoose = require('mongoose');

var todoSchema = new mongoose.Schema({
title: String,
done: Boolean,
priority: String,
deadLine: String,
masterDetailNote: String
});

module.exports = mongoose.model('Todo', todoSchema);

and in api.js 和在api.js

var Todo = require('modelDirectory/todo.js');

You missing few lines of code. 您缺少几行代码。

First in models.js add 首先在models.js添加

var User = mongoose.model('User', userSchema);

var Todo = mongoose.model('Todo', todoSchema);

module.exports = {User: User, Todo: Todo}

Then in api.js 然后在api.js

var models = require('./models.js')

var Todo = models.Todo;

Hope this helps. 希望这可以帮助。

You need to require that file in app.js and 您需要在app.js中要求该文件,然后

require('YOUR PATH')

export the file from model page 从模型页面导出文件

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

Todo.js file: Todo.js文件:

var mongoose = require("mongoose");

var Schema = mongoose.Schema;

var todoSchema = new Schema({
    title: String,
    date: String,
    completed: String,
});

module.exports = mongoose.model("todo",todoSchema);

This is my models file Todo.js.这是我的模型文件 Todo.js。 İf you wanna don't get this error, you should import your model file in your api file.如果你不想得到这个错误,你应该在你的 api 文件中导入你的模型文件。 For example I have a api file todos.js under routes file.例如,我在路由文件下有一个 api 文件todos.js I do get, post, put operation in routes/todos.js .我确实在routes/todos.js获取、发布、放置操作。 I imported models file in the todos.js .我在todos.js导入了模型文件。

var express = require('express');
var router = express.Router();
var Todo = require("../models/Todo");

router.get('/', function(req, res, next) {
  
    Todo.find().then((todos) => {
      res.json(todos);
    }).catch((err) => {
      res.json(err);
    });
  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM