Arsalan Ahmad Ishaq
Arsalan Ahmad Ishaq

Reputation: 977

cyclic dependency detected error with mongoose

I have a schema for 'Socket' model, when I try to save any created socket object in mongoose, it gives me the vague error Error: cyclic dependency detected marking only node_modules and none of my files. The schema code is as below. The way I save the socket object is also as under. Please point out the mistake if possible.

socket.js file

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const SocketSchema = new mongoose.Schema({

    _id: Schema.Types.ObjectId,
    socket_object: Schema.Types.Mixed,
    socket_id: Schema.Types.Mixed,

// other model links
    room_link: { type: Schema.Types.ObjectId, ref: 'Room' },
})

SocketSchema.pre('save', function(next) {
    this.socket_id = this.socket_object.id;
    next();
});

// SocketSchema.set('autoIndex', false);

mongoose.model('Socket', SocketSchema);

How I am saving the socket object:

let newSocket = new Socket({
    _id: new mongoose.Types.ObjectId(),
    socket_object: socket,
});      

console.log('-----------------NEW SOCKET---------------')
console.log(newSocket)

newSocket.save(function(err, newSocket){
    if (err) {
        console.log('--------------------LOG SOCKET NOT SAVED------------------------')
        return console.log(err)
    }
})

As soon as I run above code, I get the bottom vague error

Error: cyclic dependency detected at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:333:34) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:727:17) at serializeObject (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:347:18) at serializeInto (/home/arsalan/Work_stuff/Full_stack_apps/NATIVE_APPS/webRTC_fresh/webRTC_fullstack/web/node_modules/bson/lib/bson/parser/serializer.js:941:17)

please point out the mistake if possible.

Upvotes: 2

Views: 1672

Answers (1)

O. Jones
O. Jones

Reputation: 108641

A Socket object is a system object. It provides access to underlying networking capabilities provided by the host operating system.

You are trying to save that to your db. You Can't Do That™. Your object contains state -- the state of the connection to another machine in the case of TCP sockets -- that simply can't be restored if you try to retrieve the saved object later. If you want to use a similar socket later, you'll have to reconstruct it.

And, those sorts of system objects have sub-objects that point to each other. That's what makes them circular. That makes them more efficient. Serialization works best with data structures that are directed acyclic graphs.

I've needed similar functionality for a websocket-based app. I provided it with a Set of socket objects, to keep track of them in RAM. Upon opening a socket, I did socketSet.add(socket). And upon deleting it I did socketSet.delete(socket). And, when I needed to see all the open sockets I used socketSet.foreach().

Socket.io provides ways to emit messages to whole rooms or to individual connections as well.

Upvotes: 5

Related Questions