I have defined two collections in my code, Team and Player. I can’t figure out how to turn it into a JSON schema in the realm UI, with the relationships. I want the team to contain a list of players.
Looks like you’ve got part of it already. TeamSchema has players: Player[], which is correct. However, you need to add a specific field to Player to complete the relationship.
export const PlayerSchema = {
name: 'Player',
properties: {
_id: 'objectId?',
name: 'string?',
position: 'string?',
uri: 'string?',
// assignee field links the Player back to Team.players.
assignee: {
type: 'linkingObjects',
objectType: 'Team',
property: 'players'
}
},
primaryKey: '_id',
};
To actually add the player to a team, you need to query for your team. If you’re using the Realm React library, it would look something like this:
const team = useQuery(Team).filtered(`team_name == ${nameOfTeamToFind}`)[0];
After you query for your team, create a new Player object. Then, push the player to Team.players:
I noticed a couple things that might be causing the error.
Is what you shared above exactly what’s in your code? In the example you shared, you’re missing a closing bracket: team_name == ${loggedInTeam} ← need that last bracket
Is loggedInTeam an object that matches your team schema? If so, you’ll need to compare team_name to ${loggedInTeam.name}. team_name is just one property on your team schema. You could also filter by _id. That would look like this:
const team = useQuery(Team).filtered(`_id == oid(${loggedInTeam._id})`)[0];
You need oid() wrapped around _id values so that we’re comparing the same types.
@Kyle_Rollins
I just up messed up the formatting here. I didn’t forget the bracket in my code.
LoggedInTeam is not part of my Schema. LoggedInTeam was just a context string. in this example loggInTeam was a string set to ‘DC United’. So essentially loggedInTeam and ‘DC United’ is the same thing, so I was wondering why it didn’t work. Anyways, I’m doing some restructuring, so I will close the original question and open a new, if It is necessary, Thanks for the help.