flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

RELATIONSHIPS #219

Closed meladam20 closed 1 year ago

meladam20 commented 1 year ago

Hello, I'm using BelongsTo but in my nodejs server i can't get the value with req.body. Please tell me how to get the value of BelongsTo relationship.

frank06 commented 1 year ago

Show me code and show me what your server is receiving

meladam20 commented 1 year ago

@JsonSerializable() @DataRepository([JsonServerAdapter]) class User extends DataModel implements Comparable { //static toNull(_) => null; @override @JsonKey(name: '_id') final String? id; //@JsonKey(fromJson: toNull, includeIfNull: true) final String firstName; final String lastName; final String email; final String password; final BelongsTo? role;

User( {this.id, required this.firstName, required this.lastName, required this.email, required this.password, required this.role});

@override int compareTo(User other) => firstName.compareTo(other.firstName);

Map<String, dynamic> toJson() => _$UserToJson(this);

factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); }

class Role extends DataModel { //static toNull(_) => null; @override @JsonKey(name: '_id') final String? id; //@JsonKey(fromJson: toNull, includeIfNull: true) final String libelleRole;

Role({this.id, required this.libelleRole});

//@override //int compareTo(User other) => firstName.compareTo(other.firstName);

Map<String, dynamic> toJson() => _$RoleToJson(this);

factory Role.fromJson(Map<String, dynamic> json) => _$RoleFromJson(json); }

// Function de save data

Future register() async { final developerRole = Role(libelleRole: 'developpeur');

User(
    firstName: firstName.text,
    lastName: lastName.text,
    email: email.text,
    password: password.text,
    role: BelongsTo(developerRole)).save();

}

// Nodejs server code

const UserSchema = new mongoose.Schema({ //_id: Number, firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true }, password: { type: String, required: true }, role: { type: 'ObjectId', ref: 'Role', } } );

///////////////// req.body data

[ { firstName: 'az', lastName: 'er', email: 'df@gmail.com', password: '$2a$10$frDD9zJ6aphuMaTRuu4ihOQENk/SF8Vu7hyaIuXGRCTA3/drv3e1e' } ]

The role field doesn't exist and i don't know why.

frank06 commented 1 year ago

developerRole has no ID