Reputation: 545
Based on the socially project, I tried to extend the user to add a field:
export interface User extends Meteor.User {
experience: number;
}
However I get the following error:
client/imports/app/parties/party-details.component.ts (71, 7): Type 'Observable<User[]>' is not assignable to type 'Observable<User>'.
Type 'User[]' is not assignable to type 'User'.
Property 'experience' is missing in type 'User[]'.
As far as I understand that is because of the collection definition:
export const Users = MongoObservable.fromExisting(Meteor.users);
It uses Meteor.users
(without experience field
) and not my custom typescript defined version (with experience field
).
Related files:
How can I fix that? Or in other words: How do I extend the user model for an angular-meteor project?
I want to rephrase the question to: How can I extend the Meteor user collection?
export const Users = MongoObservable.fromExisting(Meteor.users);
Bonus question: And how can I initiate the field with 0
when creating new users?
Upvotes: 2
Views: 280
Reputation: 1415
However I get the following error:
You have to change line 31 to
users: Observable<User[]>;
instead of users: Observable<User>;
Although I don't even get an error thrown without changing it, so I don't know when the error is supposed to happen exactly?
Anyhow, a Meteor Collection lookup always returns an Array of objects so users: Observable<User[]>;
is correct.
How can I extend the Meteor user collection?
As for how to extend the User: Technically you wouldn't even have to extend it just to store an extra value, all the values that you give the Collection on insert will be stored for that document. What you mean by extending is, extending the Model / Schema for the User document that is to be inserted. With TypeScript this usually just is an interface but if you weren't using TS, there would be alternatives such as https://github.com/aldeed/meteor-simple-schema which actually offer to set a default value. I just say this to help you understand the concept.
An interface though does only shape values, it does not set default values or anything like that. AFAIK this is the same with interfaces in Java for example.
So what you do is extend that interface like you said:
export interface User extends Meteor.User {
experience: number;
}
This should be correct. You can not extend the "collection", the collection simply defines the collection name in the MongoDB, and possibly who can access it.
And how can I initiate the field with 0 when creating new users
As already said, you can't set default values for interfaces. All you could do is set values to optional using experience? : number
which would set it to undefined.
You can however create a class that implements that interface and then set default values.
export class SignupUser implements User{
experience:number=0;
email: string;
password: string;
}
then in your signup.component.ts
on line 29 you could do
let signupUser=new SignupUser();
signupUser.email= this.signupForm.value.email;
signupUser.password= this.signupForm.value.password;
Accounts.createUser(signupUser, err => [...])
or even create a new SignupUser right away and bind directly to those values.
Alternatively the simple way you could also just do
Accounts.createUser({
experience: 0,
email: this.signupForm.value.email,
password: this.signupForm.value.password
}
I hope this helps you.
Upvotes: 1
Reputation: 141
It looks like your issue is that somewhere you are trying to assign an Observable of a User array Observable<User[]>
to an Observable of a User Observable<User>
. Did you incorrectly apply Observable<User[]>
as the type of the MongoObservable?
Upvotes: 0