1
 {{#times league.groupNumber}}
                    <form action="/ayarlar/league/edit-{{this}}/{{../league._id}}?_method=PUT" method="POST"
                        class="boxInfo">
                        <div class="grupDiv"
                            style="width:150px;display:inline-block;border:1px solid black;padding:10px;">
                            <h2>Grup {{this}}</h2>
                            <select name="groupNo{{this}}" id="" multiple>
                                {{#each ../team}}
                                <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Warning! {{/if}}{{teamName}}</option>
                                {{/each}}
                            </select>
                            <input type="hidden" name="_method" value="PUT">
                            <button type="submit" class="greenButton">Ekle</button>
                            <ul>
                                <span style="font-weight:600;">Takımlar : </span>
                                {{#each (lookup ../league (concat 'groupNo' this))}}
                                <li>{{this.teamName}}</li>
                                {{/each}}
                            </ul>
                        </div>
                    </form>
{{/times}}

This is my code block and here is code which i had problem.

{{#each ../team}}
   <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Warning! {{/if}}{{teamName}}</option>
{{/each}}

When I run this code if the current _id is equal to league.groupNo1 elements _id , I want to see warning text before teamName. But I cannot see anything. Also When write code like this, I can see values and they are same.

{{#each ../team}}
     <option value="{{_id}}">{{_id}}</option>
     <option value="">{{../../league.groupNo1.[0]._id}}</option>
     <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Uyarı{{/if}}{{teamName}}</option>
{{/each}}

And also here is my schema's:

const LeagueSchema = new mongoose.Schema({
    leagueName : {type:String, require:true, unique: true},
    typeSelect : {type:String, require: true},
    importanceOfLeague : {type: Number, require:true},
    leagueMiddleRefereeIsChecked : {type:Boolean, default:false},
    leagueLinemanIsChecked : {type:Boolean, default:false},
    leagueSecondLinemanIsChecked: {type:Boolean, default:false},
    leagueFourthRefereeIsChecked : {type:Boolean, default:false},
    leagueVarRefereeIsChecked : {type:Boolean, default:false},
    leagueSecondVarRefereeIsChecked: {type:Boolean, default:false},
    groupNumber: {type:Number},
    groupNo1: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo2: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo3: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo4: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo5: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo6: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo7: [{ type: Schema.Types.ObjectId, ref: "team"}],
const TeamSchema = new mongoose.Schema({
    teamName: { type: String, require:true},
    leagueName: { type: Schema.Types.ObjectId, ref: "league"},
    date: { type: Date, default: Date.now },
    isTeamSelected: {type:Boolean, default:false},
})

And here is my routher.get code:


router.get("/:id",(req,res) =>{
    Team.find({ leagueName: req.params.id }).sort({teamName:1}).lean().then(team =>{
        League.findById(req.params.id)
        .populate(teamPaths)
        .then(league => {
            Team.find({ leagueName: req.params.id }).sort({teamName:1}).lean().then(team =>{
                res.render("site/leagueDataScreen", {league:league.toJSON(),team:team})
    
            })
        })
    })
    

})

I tried use lookup function but I got errors.

{{#each ../team}}
  <option value="{{_id}}">{{#if (eq _id (lookup ../../league.groupNo1.[0]._id)) }}Warning! {{/if}}{{teamName}} </option>
{{/each}}
atuna00
  • 15
  • 4
  • I am guessing that the types of the two IDs you are comparing are different. For example, one is a String and the other a Number. Can you share the `eq` helper you are using? Are you able to log the types of the arguments passed to this helper? – 76484 Mar 20 '23 at 12:40
  • here is my eq helper: `eq: function (a, b) { return a === b; }` No, I don't. How can I log? – atuna00 Mar 21 '23 at 07:48
  • To the body of the `eq` helper, add `console.log(a, typeof a); console.log(b, typeof b);` and share the output here. – 76484 Mar 21 '23 at 10:55
  • `new ObjectId("64199740971dfa9c02e94ce3") object new ObjectId("64199740971dfa9c02e94ce3") object new ObjectId("64199744971dfa9c02e94ce8") object new ObjectId("64199740971dfa9c02e94ce3") object new ObjectId("64199740971dfa9c02e94ce3") object new ObjectId("64199740971dfa9c02e94ce3") object new ObjectId("64199744971dfa9c02e94ce8") object new ObjectId("64199740971dfa9c02e94ce3") object` here is the output. – atuna00 Mar 21 '23 at 11:40
  • I think that explain the issue **before** showing the code is better than starting with the code itself – weirdgyn Mar 22 '23 at 13:49

1 Answers1

1

The comparison fails because you are comparing objects, not strings; and objects are equal when they have the same reference, which yours do not.

Leveraging this post about comparing Mongoose Object IDs (which I would recommend you read and up-vote if it is helpful to you), I would suggest creating a new Handlebars Helper to compare Object IDs. For example,

eqIds: function (id1, id2) {
  return id1.equals(id2);
}
76484
  • 8,498
  • 3
  • 19
  • 30
  • 1
    Thank you and also I will read this post. If ı want to take all _id values for groupNo1 element's what change do I need to make on my codes? Code means to _id is equal any of league.groupNo1.[x]_id. For example, `(eqIds _id ../../league.groupNo1.[0]._id ), (eqIds _id ../../league.groupNo1.[1]._id ), (eqIds _id ../../league.groupNo1.[2]._id )` Is there any abbreviation for this? I hope I have explained myself. – atuna00 Mar 21 '23 at 13:02
  • 1
    @atuna00: What you asking is quite a bit more involved and really needs a new question. You would need to create a helper that would loop through all of the teams in the group to see if there is an ID match. See: https://jsfiddle.net/76484/g4uo1kbf/ – 76484 Mar 21 '23 at 13:23