Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link cats with users #145

Open
felixchristo opened this issue Dec 26, 2017 · 18 comments
Open

Link cats with users #145

felixchristo opened this issue Dec 26, 2017 · 18 comments
Labels

Comments

@felixchristo
Copy link

How to maintain session with this so that each user can maintain their own data

@DavideViolante
Copy link
Owner

The session of the login is already mantained with the token in localstorage. Each user that login have its own token.

@felixchristo
Copy link
Author

Yes sir, But the data of one user is visible to other. How to show the data which is related to that particular user alone

@DavideViolante
Copy link
Owner

But the data of one user is visible to other

What do you mean? Tell me what you need to do in details.

@felixchristo
Copy link
Author

felixchristo commented Dec 27, 2017

If X login and Enter details and then Y login, Y able to see X's details and X able to see Y's details.

@dougal83
Copy link

dougal83 commented Dec 27, 2017 via email

@felixchristo
Copy link
Author

I think I need to edit base.js for that?

@felixchristo
Copy link
Author

If you could do that and update it, It will be very useful to all and the project will look meaningful

@dougal83
Copy link

dougal83 commented Dec 27, 2017 via email

@DavideViolante
Copy link
Owner

DavideViolante commented Dec 27, 2017

The user data is private, the cat page and data is visible to all users, including the guests. I though about the cats as the "products" that can be seen by everyone. Currently there is no association between users and cats, since this is just a starter project and is meant to be simple.
If you want to create a link between them, you should edit the user schema as follows:
cats: [{ type: Schema.Types.ObjectId, ref: 'Cat' }]
The important word here to note is ref. This creates a reference between User schema and Cat schema. So each user in this way will have an array of cats, their cats.
You can find some tutorials online about this topic, search for "mongoose ref"
http://mongoosejs.com/docs/populate.html

@felixchristo
Copy link
Author

I am just getting this
"_id" : ObjectId("5a446ab43533970b8489e1ac"),
"username" : "Chris",
"email" : "[email protected]",
"password" : "$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu",
"role" : "user",
"cats" : [ ],
"__v" : 0

If I do cats: [{ type: Schema.Types.ObjectId, ref: 'Cat' }] in user.js

@DavideViolante
Copy link
Owner

It's not that simple, I just gave you an hint. Every time you do the add new cat you should update the cats array of the user that added that cat.

@DavideViolante DavideViolante changed the title Maintain Session Link cats with users Feb 18, 2018
@felixchristo
Copy link
Author

Still I couldn't do this :(

@issambaccouch
Copy link

issambaccouch commented Feb 22, 2018

you should add createdBy in cat model , save userid on it then use ngif to filter cats list it's already worked for me :
1- in authService for me i call it current
code :
get current() {
return this.currentUser;
}
2- in cat component add
current = this.authService.current._id;
add in ngOninit() :
this.addCatForm = this.formBuilder.group({
.
.
.
createdBy: this.authService.current._id
});

3- in cat html file use this condition :
code :
tbody *ngIf="!isEditing"
tr *ngFor="let cat of cats"
span *ngIf="current == cat.createdBy"
td>{{cat.title}}</td
td>{{cat.content}}</td
td>
#note : don't forget to import the services and add createdby in ur server side

@felixchristo
Copy link
Author

felixchristo commented Feb 23, 2018

logically sounds good buddy.. Let me try and get back to you

@felixchristo
Copy link
Author

yeaahhh it's working but the thing has to be done from server side buddy not from client side, which is not safe..

@dougal83
Copy link

dougal83 commented Feb 28, 2018 via email

@adwulfran
Copy link
Contributor

I think you can use the save(user) function ! I wanted to do same thing as you and I did it like that :

1)Add array property cats to user.model ( as davideviolante said above you can also use populate method, which may be more appropriate)
2) In cats.component.html add form (ngSubmit)="save(user)" and input [(ngModel)]="user.cats"
3)In cats.component.ts : import user.modele and user.service
4) In cats.component.ts : add getUser() and save(user: User)
5)Add {{user.cats}} to cats.component.html

you can check my repo I did it with film.detail.component.ts (instead of your cats I used films)

@wxyzed
Copy link

wxyzed commented May 7, 2019

yeaahhh it's working but the thing has to be done from server side buddy not from client side, which is not safe..

Can you provide me with the project after you modifided it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants