-2
\$\begingroup\$

i know this question has been asked a lot but none of the solutions of those questions solved my problem . i am trying to join two collections and use the populate method but the populate method seems to not working at all

I tried to change the collections name , like adding a 's' at the end but didn't worked , i also created the collections from scratch but it didn't helped

Below is my controller file . In education function , when i am trying to find chats belonging to particular category and also populating it with collection name i want, which is 'user' but in the chat object it is only having the data from chat collection and not from user collection

       const user = require('../models/model1').user;
       const chat = require('../models/model1').chat;


        const education = function (req, res) {

        chat.find({ categorie: "/education"}).
        populate('user').
        exec(function (err, chat) {
        if (err) return handleError(err);
        console.log( "&&&&&&&&&&&&&&&& ",chat);
        res.status(200).render('education',{message:chat});
         });
       };


       const message = function (req, res) { // storing the 50 
       previous messages

      var new_chat;
       user.find(
         {email: req.query.y}, (err, user) =>{
              if (err) return res.status(500).send(err);

           var new_chat = new chat({
               body: req.query.x,
               date:    new Date(),
               sender: user[0].id,
               categorie: req.query.z
           });
           new_chat.save(function(err) {
               if (err) throw err;
                  console.log('message saved');
           });
        });
         };

        exports=module.exports={education,message};

then this is my schema file

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

       let user_schema = new Schema({
       email: {type: String, required: true, max: 100, unique          
        :true},
        password: {type: String, required: true},
        username: {type: String},
        friends:{type: Schema.Types.ObjectId, ref: 'user'}
         });

         const user = mongoose.model('user', user_schema,'user');
         let chat_schema=new Schema({

         body :{type: String ,reqiured: true},
         date: {type:Date , reqiured: true},
          sender:{type: Schema.Types.ObjectId, ref: 'user', required: 
          true},
           categorie: {type: String, required: true},
           receiver:{type: Schema.Types.ObjectId, ref: 'user',}
            });

         const chat = mongoose.model('chat',chat_schema,'chat' );

          exports =module.exports={chat,user};
New contributor
Mrinal Verma is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

closed as off-topic by 200_success, user673679, Sᴀᴍ Onᴇᴌᴀ, Toby Speight, Heslacher Feb 12 at 4:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, user673679, Sᴀᴍ Onᴇᴌᴀ, Toby Speight, Heslacher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ can someone also tell me why my question down-voted,i am a new user who is not the much aware of things work in this site but instead of helping me and pointing out my mistake people straight away down-voted my answer , its really sad and unexpected to me that some people from this community are like that.If you didn't like something in my question or was not able to understand then ask me , drop a comment . i spent whole day figuring this error and at night asked the question, so please help do not demotivate \$\endgroup\$ – Mrinal Verma Feb 11 at 20:40
  • \$\begingroup\$ Codereview is for reviewing working code. As such this question is off-topic here. For code that doesn't work, try stackoverflow.com . Note that you will likely be asked to add more detail about what "doesn't work" means (e.g. is there an error? does it run but the output is unexpected? etc.) \$\endgroup\$ – user673679 Feb 11 at 20:55
  • 1
    \$\begingroup\$ Have a look at What topics can I ask about here? for what you can expect from this community, too. \$\endgroup\$ – greybeard Feb 11 at 21:26
  • \$\begingroup\$ @user673679 and greybeard thanks for guiding me , i will ask this question on the appropriate platform , and sorry for any inconvenience \$\endgroup\$ – Mrinal Verma Feb 11 at 21:34

Browse other questions tagged or ask your own question.