-1

My mysql table models :

//mysql
//table structures start

var Contact = sequelize.define('contact', {
    gsm: {
        type: Sequelize.STRING,
        unique: true,
        required: true
    },
    firstName: Sequelize.STRING,
    lastName: Sequelize.STRING,
    street: Sequelize.STRING,
    city: Sequelize.STRING,
})

var Group = sequelize.define('group', {
    groupname: Sequelize.STRING

})

var ContactGroup = sequelize.define('contactgroup', {
    /* Empty */
})

ContactGroup.belongsTo(Contact, {
    onDelete: 'CASCADE'
});

Contact.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

ContactGroup.belongsTo(Group, {
    onDelete: 'CASCADE'
});
Group.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

This is my first api code:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id']
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

I got the first api response like this:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]

This is my second api code:

exports.getNewGroupForProfsms = (req, res) => {
    Group.findAll({
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

This is my second api response:

[
    {
        "id": 14,
        "groupname": "Angular",
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

In above code, i wrote two apis, now i want to write single api,

I need this type of response: (i have contactsCount in first api response, i want that in second api response)

Finally I want this type of response with single api code:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

will anyone help me? i need single api code.

I tried nested query, and modified my first api code like this:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

But i got this type of response not my requirement

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]
7
  • you can nest query group in side first api, or just use right join in your sql, but for better performance and design please refer docs.sequelizejs.com/manual/tutorial/… Commented Dec 21, 2017 at 7:47
  • can you post some piece of code? if i nested query in my first api, i did not got correct response, wait i will post my nested query code which i tried. Commented Dec 21, 2017 at 8:00
  • @JoshLin see my updated question, i wrote my nested include code. Commented Dec 21, 2017 at 8:06
  • you only want contactsCount in second API? something like res.send(data.map((x)=>{...x,contactsCount:x.contactgroups.length}))? Commented Dec 21, 2017 at 8:58
  • @JoshLin I am new to nodejs, where i can post this code? yes i need only contact count, can you post some answer? Commented Dec 21, 2017 at 9:01

1 Answer 1

1

try this code

exports.getNewGroupForProfsms = (req, res) => {
  Group.findAll({
    include: [{
      model: ContactGroup,
      attributes: ['id'],
      include: [{
        model: Contact,
        attributes: ['id', 'gsm']
      }]
    }],
  }).then(data => {
    // change happen here
    return res.status(200).send(data.map((x) => {
      // assign contactsCount to each row
      return Object.assign({
        contactsCount: x.contactgroups.length,
      }, x.toJSON()) // update toJSON have a try
    }));
  }).catch(err => {
    return res.status(400).send(err.message);
  });
};

Sign up to request clarification or add additional context in comments.

8 Comments

not working, still i dont get contactsCount in my response
i have contactsCount in exports.getnewgroup api.
Still my response looks like hastebin.com/irahimuluw.json :) i did not get contactsCount
Object.assign({ contactsCount: x.contactgroups.length, }, x) assigned to new object, if it still not working, use .send(data.map(()=>'aaa')) to check if response changes
i checked using .send(data.map(()=>'aaa')) . i got [ "aaa", "aaa", "aaa", "aaa", "aaa" ]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.