Data structure of Threaded comments in mongoose and Node Js - javascript

{
"post": {
"img": "",
"likes": [
"60c418582f7066090ced4a51"
],
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [],
"_id": "60d6ab9207c0a573786a9e65",
"userId": "60c418582f7066090ced4a51",
"content": "good post 3",
"createdAt": "2021-06-26T04:22:42.337Z",
"updatedAt": "2021-06-26T04:22:42.337Z",
"__v": 0
}
],
"_id": "60d6962cee10aa73f820b974",
"userId": "60c418582f7066090ced4a51",
"content": "good post 2",
"createdAt": "2021-06-26T02:51:24.111Z",
"updatedAt": "2021-06-26T04:22:42.622Z",
"__v": 0
},
{
"comments": [],
"_id": "60d705784ab01c354cf7f445",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "Comment deleted by User",
"createdAt": "2021-06-26T10:46:16.813Z",
"updatedAt": "2021-06-26T12:29:06.398Z",
"__v": 0
},
{
"comments": [],
"_id": "60d706febcba957b04406547",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "yes it is a good post 1 from alexane Updated",
"createdAt": "2021-06-26T10:52:46.679Z",
"updatedAt": "2021-06-26T12:17:58.879Z",
"__v": 0
}
],
"_id": "60d695b4ee10aa73f820b973",
"userId": "60c418582f7066090ced4a51",
"content": "good post 1",
"createdAt": "2021-06-26T02:49:24.426Z",
"updatedAt": "2021-06-26T12:30:44.872Z",
"__v": 0
}
],
"_id": "60d68e32dff84439a4d3b191",
"userId": "60c418582f7066090ced4a51",
"content": "good post",
"createdAt": "2021-06-26T02:17:22.625Z",
"updatedAt": "2021-06-26T02:49:24.820Z",
"__v": 0
},
{
"comments": [
{
"comments": [],
"_id": "60d6c2d917d0b12be44742d2",
"userId": "60c418582f7066090ced4a51",
"content": "nice post 1",
"createdAt": "2021-06-26T06:02:01.420Z",
"updatedAt": "2021-06-26T06:02:01.420Z",
"__v": 0
}
],
"_id": "60d6bebf17d0b12be44742d1",
"userId": "60c418582f7066090ced4a51",
"content": "nice post",
"createdAt": "2021-06-26T05:44:31.436Z",
"updatedAt": "2021-06-26T06:02:01.676Z",
"__v": 0
},
{
"comments": [
{
"comments": [],
"_id": "60d87e7df43fed7e4079875e",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "awesome post 1",
"createdAt": "2021-06-27T13:34:53.192Z",
"updatedAt": "2021-06-27T13:34:53.192Z",
"__v": 0
}
],
"_id": "60d87cb4f43fed7e4079875d",
"userId": "60c418582f7066090ced4a51",
"content": "awesome post",
"createdAt": "2021-06-27T13:27:16.299Z",
"updatedAt": "2021-06-27T13:34:53.468Z",
"__v": 0
}
],
"_id": "60c5a23eb37b425a00968fa9",
"userId": "60c418582f7066090ced4a51",
"description": "This is a sample Post 2",
"createdAt": "2021-06-13T06:14:22.196Z",
"updatedAt": "2021-06-27T13:27:16.577Z",
"__v": 0
}
}
I have a threaded comments for a single post which I recursively autopopulated using Mongoose (the code can be found below ). Is it correct to assume the above data structure is a Tree ? If yes, what kind of tree data structure would be the best to implement a a threaded comments to do basic crud such as insert , edit and delete.
//Comment.js
const mongoose = require('mongoose')
const { Schema } = mongoose
const commentObj = {
userId: {
type: mongoose.Types.ObjectId,
ref: "User",
required: true
},
content: {
type: String,
min: 6,
required: true
},
comments: [
{ type: Schema.Types.ObjectId, ref: 'Comment' }
],
}
const commentSchema = new Schema(commentObj, { timestamps: true });
const autoPopulateChildren = function (next) {
this.populate('comments');
next();
};
commentSchema
.pre('findOne', autoPopulateChildren)
.pre('find', autoPopulateChildren)
const Comment = mongoose.model('Comment', commentSchema);
module.exports = Comment
//Post.js
const mongoose = require('mongoose')
const { Schema } = mongoose
const postObj = {
userId: {
type: mongoose.Types.ObjectId,
ref: "User",
required: true
},
description: {
type: String,
min: 6,
required: true
},
img: {
type: String,
default: '',
},
likes: [
{
type: mongoose.Types.ObjectId,
ref: "User"
}
],
comments: [
{
type: mongoose.Types.ObjectId,
ref: "Comment",
}
]
}
const postSchema = new Schema(postObj, { timestamps: true });
const Post = mongoose.model('Post', postSchema);
module.exports = Post
Thank you.

Related

Add a Object into state with array of Object by Redux

when my backend send a payload with value:
Object {
"__v": 0,
"_id": "621ef5eec33b5c6d9d184563",
"category": "621ef5e8c33b5c6d9d18455e",
"createdAt": "2022-03-02T04:43:26.834Z",
"description": "",
"name": "Hair and Nails",
"price": 50,
"updatedAt": "2022-03-02T04:43:26.834Z",
},
How to add this Object to my serviceReducer
const initialState = {
isLoading: false,
error: false,
serviceCategories: [], //! serviceCategories is a Array
};
first of all, I map serviceCategories and selected serviceCategories that I need
return {
...state,
serviceCategories: state.serviceCategories.map((serviceCateogory) => {
return serviceCateogory._id === action.payload.category
? serviceCateogory //! here How I to add that object to services of serviceCateogory
: serviceCateogory;
}),
// ),
};
this is Structure of serviceCategories:
[
Object {
"__v": 0,
"_id": "621ef5e8c33b5c6d9d18455e",
"createdAt": "2022-03-02T04:43:20.754Z",
"name": "Category1",
"services": Array [
Object {
"__v": 0,
"_id": "621ef5eec33b5c6d9d184563",
"category": "621ef5e8c33b5c6d9d18455e",
"createdAt": "2022-03-02T04:43:26.834Z",
"description": "",
"name": "Service1",
"price": 50,
"updatedAt": "2022-03-02T04:43:26.834Z",
},
Object {
"__v": 0,
"_id": "621ef7d1c33b5c6d9d1845b1",
"category": "621ef5e8c33b5c6d9d18455e",
"createdAt": "2022-03-02T04:51:29.262Z",
"description": "",
"name": "Service2",
"price": 50,
"updatedAt": "2022-03-02T04:51:29.262Z",
},
],
"updatedAt": "2022-03-02T05:08:35.520Z",
},
Object {
"__v": 0,
"_id": "621ef5e8c33b5c6d9d18455e",
"createdAt": "2022-03-02T04:43:20.754Z",
"name": "Category2",
....
]
You need to first findIndex of the specific category from the redux state and then append the service in it.
Like:
let categories=[...state.serviceCategories];
let catIndex=categories.findIndex((item)=>item._id===action.payload.category);
if(catIndex!=-1) categories[catIndex].services.push(object); // append object (service) as you wish.
state.serviceCategories=categories;

Nested comments in comments

const post = {
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [],
"_id": "60d6ab9207c0a573786a9e65",
"userId": "60c418582f7066090ced4a51",
"content": "good post 3",
"createdAt": "2021-06-26T04:22:42.337Z",
"updatedAt": "2021-06-26T04:22:42.337Z",
"__v": 0
}
],
"_id": "60d6962cee10aa73f820b974",
"userId": "60c418582f7066090ced4a51",
"content": "good post 2",
"createdAt": "2021-06-26T02:51:24.111Z",
"updatedAt": "2021-06-26T04:22:42.622Z",
"__v": 0
},
{
"comments": [],
"_id": "60d705784ab01c354cf7f445",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "Comment deleted by User",
"createdAt": "2021-06-26T10:46:16.813Z",
"updatedAt": "2021-06-26T12:29:06.398Z",
"__v": 0
},
{
"comments": [],
"_id": "60d706febcba957b04406547",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "yes it is a good post 1 from alexane Updated",
"createdAt": "2021-06-26T10:52:46.679Z",
"updatedAt": "2021-06-26T12:17:58.879Z",
"__v": 0
}
],
"_id": "60d695b4ee10aa73f820b973",
"userId": "60c418582f7066090ced4a51",
"content": "good post 1",
"createdAt": "2021-06-26T02:49:24.426Z",
"updatedAt": "2021-06-26T12:30:44.872Z",
"__v": 0
}
],
"_id": "60d68e32dff84439a4d3b191",
"userId": "60c418582f7066090ced4a51",
"content": "good post",
"createdAt": "2021-06-26T02:17:22.625Z",
"updatedAt": "2021-06-26T02:49:24.820Z",
"__v": 0
},
{
"comments": [
{
"comments": [],
"_id": "60d6c2d917d0b12be44742d2",
"userId": "60c418582f7066090ced4a51",
"content": "nice post 1",
"createdAt": "2021-06-26T06:02:01.420Z",
"updatedAt": "2021-06-26T06:02:01.420Z",
"__v": 0
}
],
"_id": "60d6bebf17d0b12be44742d1",
"userId": "60c418582f7066090ced4a51",
"content": "nice post",
"createdAt": "2021-06-26T05:44:31.436Z",
"updatedAt": "2021-06-26T06:02:01.676Z",
"__v": 0
}
]
}
I have a deep nested object which I got from mongoose.
a. How do I iterate through this deep nested objects? Should I use a recursion and loop because I can't seem to wrap my head around this? I manage to iterate through to the end of the comments . I managed to come up with something as brute force as below but i am kinda stuck here.
const findObject = (obj) => {
for (let i = 0; i < obj.comments.length; i++) {
const element = obj.comments[i];
for (let y = 0; y < element.comments.length; y++) {
const newElements = element.comments[y];
for (let z = 0; z < newElements.comments.length; z++) {
const newElementsZ = newElements.comments[z];
console.log(newElementsZ)
}
}
}
};
b. How to calculate the total number of comments, in this case it is 8 comments ?
Thank you very much.
You can use recursion to do it. (since it has indefinite number of layers, write all the nested loops doesn't really scale).
const post = { "comments": [{ "comments": [{ "comments": [{ "comments": [{ "comments": [], "_id": "60d6ab9207c0a573786a9e65", }], "_id": "60d6962cee10aa73f820b974", }, { "comments": [], "_id": "60d705784ab01c354cf7f445", }, { "comments": [], "_id": "60d706febcba957b04406547", } ], "_id": "60d695b4ee10aa73f820b973", }], "_id": "60d68e32dff84439a4d3b191", }, { "comments": [{ "comments": [], "_id": "60d6c2d917d0b12be44742d2", }], "_id": "60d6bebf17d0b12be44742d1", } ] }
function CountComment(data){
let count = 0
for(let c of data.comments){
++count
count+=CountComment(c)
}
return count
}
console.log(CountComment(post))
Or you can use stack/queue based approach if the recursion depth cause problem.
const post = { "comments": [{ "comments": [{ "comments": [{ "comments": [{ "comments": [], "_id": "60d6ab9207c0a573786a9e65", }], "_id": "60d6962cee10aa73f820b974", }, { "comments": [], "_id": "60d705784ab01c354cf7f445", }, { "comments": [], "_id": "60d706febcba957b04406547", } ], "_id": "60d695b4ee10aa73f820b973", }], "_id": "60d68e32dff84439a4d3b191", }, { "comments": [{ "comments": [], "_id": "60d6c2d917d0b12be44742d2", }], "_id": "60d6bebf17d0b12be44742d1", } ] }
function CountComment(post){
let nodes = [post]
let count = 0
while(nodes.length){
let node = nodes.pop()
++count
for(let c of node.comments)
nodes.push(c)
}
return count-1 //remove post itself
}
console.log(CountComment(post))

How can I get count of the Documents and filter them in efficient way? (mongoose)

I'm implementing search function that is simply find document in mongoDB. I want to .skip(x) and .limit(x) on result to simulate paging result, but can I get total count of document (before skip and limit) and get filtered result at once?
Code that produce Expected Output :
db.Datas.find({ type: "Unknown" })
.then((result) => {
let count = result.length;
db.Datas.find({ type: "Unknown" })
.sort({ createdAt: -1 })
.skip((req.query.page - 1) * 10)
.limit(10)
.then((res) => {
res.json({ count: count, result: res });
});
})
.catch((err) => {});
But querying twice it somehow annoying, and it might be slow at large database.
I tried something like find({}).then(x => { ... }).sort(...) ... but isn't working because it only returns Promise.
How can I do this things in efficient way?
or is just getting whole documents and skip, limit with JS-way (using .splice, or etc..) will be faster and efficient?
You can use $facet aggregation to achieve this.
db.Datas.aggregate([
{
$match: {
"type": "Unknown"
}
},
{
$sort: {
createdAt: -1
}
},
{
$facet: {
totalRecords: [
{
$count: "total"
}
],
data: [
{
$skip: 0
},
{
$limit: 5
}
]
}
}
])
Playground
Let's say you have these documents:
db={
"Datas": [
{
"_id": "5e390fc33285e463a0799689",
"type": "Known",
"createdAt": "2020-02-04T06:31:31.311Z",
"__v": 0
},
{
"_id": "5e390fd03285e463a079968a",
"type": "Known",
"createdAt": "2020-02-04T06:31:44.190Z",
"__v": 0
},
{
"_id": "5e390fda3285e463a079968b",
"type": "Unknown",
"createdAt": "2020-02-04T06:31:54.248Z",
"__v": 0
},
{
"_id": "5e390fdf3285e463a079968c",
"type": "Unknown",
"createdAt": "2020-02-04T06:31:59.993Z",
"__v": 0
},
{
"_id": "5e390fec3285e463a079968d",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:12.336Z",
"__v": 0
},
{
"_id": "5e390ffd3285e463a079968e",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:29.670Z",
"__v": 0
},
{
"_id": "5e3910163285e463a079968f",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:54.131Z",
"__v": 0
},
{
"_id": "5e3910213285e463a0799690",
"type": "Unknown",
"createdAt": "2020-02-04T06:33:05.166Z",
"__v": 0
}
]
}
Response will be like this:
[
{
"data": [
{
"__v": 0,
"_id": "5e3910213285e463a0799690",
"createdAt": "2020-02-04T06:33:05.166Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e3910163285e463a079968f",
"createdAt": "2020-02-04T06:32:54.131Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390ffd3285e463a079968e",
"createdAt": "2020-02-04T06:32:29.670Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390fec3285e463a079968d",
"createdAt": "2020-02-04T06:32:12.336Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390fdf3285e463a079968c",
"createdAt": "2020-02-04T06:31:59.993Z",
"type": "Unknown"
}
],
"totalRecords": [
{
"total": 6
}
]
}
]
As you see, we got the total records with the filtered, sorted, skipped and limited data.

Creating a new table in mongoose based on object IDs

Hello the title of this question is very poorly worded however i will better explain the issue.
I have a document called 'buildings', and I also have two documents called 'rooms' and 'logins'. In both rooms and logins, they have an embedded document of building as follows:
Room Schema:
const roomSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
building: {
type: new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
}
}),
required: true
}
});
Logins Schema:
const loginSchema = new mongoose.Schema({
user: {
type: new mongoose.Schema({
email: {
type: String,
required: true
},
isVisitor: {
type: Boolean,
required: true
}
})
},
dateIn: {
type: Date,
required: true,
default: Date.now()
},
dateOut: {
type: Date
},
building: {
type: new mongoose.Schema({
name: {
type: String,
required: true
}
})
}
});
And the building schema is as follows:
const Building = mongoose.model(
"Building",
new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
address: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
postcode: {
type: String,
required: true,
minlength: 6,
maxlength: 8
},
organisation: {
type: new mongoose.Schema({
name: {
type: String,
required: true
}
})
}
})
);
Here is some examples of documents from each schema:
Building:
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "TusPark Newcastle"
},
"__v": 0
},
Rooms:
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark"
},
"__v": 0
},
Logins:
{
"dateIn": "2019-03-21T13:13:23.069Z",
"_id": "5c938e151bd7a02d479893bd",
"user": {
"_id": "5c925378e88bb72764283108",
"email": "jack1#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark"
},
"__v": 0
}
I am trying to send a document to the client which contains the building details, with the rooms and logins embedded as attributes for the relevant building. The code I currently have written for this is below:
router.get("/all", async (req, res) => {
const buildings = await Building.find();
const rooms = await Room.find({
building_id: buildings._id
});
const logins = await Login.find({
building_id: buildings._id
});
const building_rooms = await Building.aggregate([
{
$lookup: {
from: "rooms",
localField: "building_id",
foreignField: "building_id",
as: "building_rooms"
}
},
{
$lookup: {
from: "logins",
localField: "building_id",
foreignField: "building_id",
as: "building_logins"
}
}
]);
res.send(building_rooms);
});
Note I have purposely not included $unwind
The current output for this request is:
[
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "Park Newcastle"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
},
{
"_id": "5c925475e88bb72764283113",
"name": "Manchester",
"site": {
"_id": "5c921c94a922f6236cbe37d2",
"name": "Manchester"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
}
]
However the issue is that the logins and rooms of 'Park' are being embedded in manchester building. which is not correct.
The correct output should be:
[
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "Park Newcastle"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
},
{
"_id": "5c925475e88bb72764283113",
"name": "Manchester",
"site": {
"_id": "5c921c94a922f6236cbe37d2",
"name": "Manchester"
},
"__v": 0,
"building_rooms": [],
"building_logins": []
}
]
With there being no instances of building_rooms or building_logs as there are no logins or rooms with the object ID relating to manchester.
I really appreciate the time taken to read this and would be grateful for any help with solving tis problem.

sequelize include returns only one result

I have a Users table and a Groups table, when I retrieve a single user, I want to get a list of all the groups that user belongs to. I created a joins table named GroupUsers and it has userId and groupId on it, this is how I know that a user belongs to a group. Now, when I try to get the list of groups a user belongs to using the sequelize include on find, it only returns the first match, if the user belongs to various groups. How do I solve this?
Users controller
return models.Users
.find({
include: [{
model: models.Groups,
as: 'groups',
limit: null,
required: false
}],
where: { username }
})
Returns this:
{
"id": 1,
"username": "John",
"phone": "xxxxxxxx",
"email": "john#email.com",
"createdAt": "2017-07-16T20:14:04.744Z",
"updatedAt": "2017-07-16T20:14:04.744Z",
"groups": [
{
"id": 1,
"name": "Test Group",
"type": "Public",
"createdAt": "2017-07-16T20:13:18.392Z",
"updatedAt": "2017-07-16T20:13:18.392Z",
"GroupUsers": {
"userId": 1,
"groupId": 1,
"last_seen": null,
"createdAt": "2017-07-16T20:14:27.903Z",
"updatedAt": "2017-07-16T20:14:27.903Z"
}
}
]
}
Instead of this:
{
"id": 1,
"username": "John",
"phone": "xxxxxxxx",
"email": "john#email.com",
"createdAt": "2017-07-16T20:14:04.744Z",
"updatedAt": "2017-07-16T20:14:04.744Z",
"groups": [
{
"id": 1,
"name": "Test Group",
"type": "Public",
"createdAt": "2017-07-16T20:13:18.392Z",
"updatedAt": "2017-07-16T20:13:18.392Z",
"GroupUsers": {
"userId": 1,
"groupId": 1,
"last_seen": null,
"createdAt": "2017-07-16T20:14:27.903Z",
"updatedAt": "2017-07-16T20:14:27.903Z"
}
},
{
"id": 2,
"name": "Test Group 2",
"type": "Public",
"createdAt": "2017-07-16T20:13:18.392Z",
"updatedAt": "2017-07-16T20:13:18.392Z",
"GroupUsers": {
"userId": 1,
"groupId": 2,
"last_seen": null,
"createdAt": "2017-07-16T20:14:27.903Z",
"updatedAt": "2017-07-16T20:14:27.903Z"
}
}
]
}
I'm sure I'm doing something wrong somewhere I just don't know where, that same thing may also be the cause of sequelize including the joins table in the result: GroupUsers
It appears that in my associations, I did:
Users.belongsToMany(models.Groups, {
through: 'GroupUsers',
as: 'groups',
foreignKey: 'groupId'
});
Instead of :
Users.belongsToMany(models.Groups, {
through: 'GroupUsers',
as: 'groups',
foreignKey: 'userId'
});
Note the foreignKey attribute
And as for the GroupUsers object that is also returned, I removed that by doing:
include: [{
model: models.Groups,
as: 'groups',
attributes: ['id', 'name'],
through: { attributes: [] }
}]
Note the through key which has attributes set to an empty array.

Categories

Resources