猫鼬:检测到循环依赖

时间:2018-06-18 04:42:56

标签: express mongoose

我有一个Express / Mongoose应用程序,不断向我发送“循环依赖检测”错误。我已将问题本地化为其中一个文件。

app.js

var express = require('express');

var mongoose = require('mongoose');
mongoose.connect('mongodb+srv://username:MY.PASSWORD@cluster0-6vlss.mongodb.net/test?retryWrites=true');

require('./models/Users');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

路由/ users.js

var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();

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

router.route('/user/:id')
    .get((req, res, next) => {
        User.findById(req.params.id, (err, user) => {
            if (err) return next(err);
            res.json(user);
        });
    });

模型/ Users.js

var mongoose = require('mongoose');

var UserSchema = mongoose.Schema({
    _id: String,
    username: {type: String, unique: true},
    hash: String,
    salt: String,
    chats: Array
});

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

希望我能够很好地压缩我的代码。有人能指出我搞砸了吗?

1 个答案:

答案 0 :(得分:0)

知道了。在app.js中,dbconfig是存储数据库URI字符串的对象。我打电话给#34; dbconfig"直接代替" dbconfig.url"。现在一切都很好。