【问题标题】:(node:10544) Accessing non-existent property '...' of module exports inside circular dependency - MongoDB (Mongoose) - NodeJS (Express)(节点:10544)访问循环依赖中模块导出的不存在属性“...”-MongoDB(Mongoose)-NodeJS(Express)
【发布时间】:2022-06-13 07:43:31
【问题描述】:

我正在 NodeJS (Express 4.17.3) 中使用 mongoDB (mongoose 6.3.0) 为移动应用构建 API。

结构 → Structure

在这个 API 中我有:

  • 用户
  • 气泡

一个用户可以参与多个泡泡,一个泡泡可以有多个用户。 用户引用了他参与的所有bubble_id。 气泡具有对参与气泡的所有 user_id 的引用。

我的 user.model.js

    username: {
        type: String,
        required: true,
    },
    local: {
        email: {
            type: String,
            required: true,
            unique: true,
        },
        password: {
            type: String,
            required: true,
        },
    },
    bubbles: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Bubble',
        required: false,
    }],
}, {
    versionKey: false,
});

我的 bubble.model.js

    name: {
        type: String,
        required: true,
    },
    dateCreation: Date,
    users: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true,
    }],
    userAdmin:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true,
    }
}, {
    versionKey: false,
}); 

问题是,我创建了一个将用户添加到气泡并更新用户的“气泡”键的方法,我的气泡也有相同的方法,我创建了一个将气泡添加到用户并更新的方法我的气泡的“用户”键。

我的 user.queries.js

const { addUserToBubble } = require("./bubbles.queries");

exports.addBubbleToUser = async (userId, body) => {
    const bubbleId = body.bubbleId;
    addUserToBubble(userId, bubbleId)
    
    return User.findByIdAndUpdate(
        userId, {
            $push: {
                bubbles: bubbleId,
            },
        }, {
            new: true,
        }
    );
};

我的 bubbles.queries.js

const { addBubbleToUser } = require('./users.queries')

exports.addUserToBubble = async (bubbleId, body) => {
    const userId = body.userId;
    addBubbleToUser(userId, bubbleId)
    
    return Bubble.findByIdAndUpdate(
        bubbleId, {
            $push: {
                users: userId,
            },
        }, {
            new: true,
        }
    );
};

我知道我的结构中有一些东西,但我无法弄清楚。 当我启动我的服务器有一个警告: 警告:在循环依赖中访问模块导出的不存在属性“addUserToBubble”

我该如何解决我的问题? 谢谢你的时间:)

好的,我做了一些类似的更新,它可以工作:

//method to add a user to a bubble update the bubble collection and user collection
exports.addUserToBubble = async (userId, body) => {
    const user = await User.findById(userId);
    const bubble = await Bubble.findById(body.bubbleId);
    const index = user.bubbles.indexOf(body.bubbleId);
    if (index > -1) {
        user.bubbles.splice(index, 1);
    }
    user.bubbles.push(body.bubbleId);
    bubble.users.push(userId);

    return Promise.all([
        Bubble.findByIdAndUpdate(
            body.bubbleId, {
                $addToSet: {
                    users: bubble.users,
                },
            }, {
                new: true,
            }
        ),
        User.findByIdAndUpdate(
            userId, {
                $addToSet: {
                    bubbles: user.bubbles,
                },
            }, {
                new: true,
            }
        ),
    ]);
}

【问题讨论】:

    标签: node.js mongodb express mongoose circular-dependency


    【解决方案1】:

    我有类似的问题。解决方案是当需要 Mongoose 模型时不要使用 CONT,而是使用 VAR 或 LET。示例 'let/var {FarmersDB} = require('./farmer_Schema')'

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 2021-08-21
      • 2021-02-13
      • 2021-02-19
      • 2021-05-16
      • 2021-05-17
      • 1970-01-01
      • 2021-08-01
      • 2021-03-01
      相关资源
      最近更新 更多