From 54325fb17c71cae33f5e8d7f4e967b788c012c43 Mon Sep 17 00:00:00 2001 From: vishwesh Date: Mon, 8 Apr 2019 00:18:13 +0530 Subject: [PATCH 1/2] resolved server crashes on hitting like button in common component --- .../common-feed/common-feed.component.ts | 21 ++++++++++++++----- .../app/secure/shared/post/post.component.ts | 14 ++++++++++--- stackle-app/src/app/services/post.service.ts | 4 ++-- stackle_api/app/models/post.js | 6 +++++- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/stackle-app/src/app/secure/common-feed/common-feed.component.ts b/stackle-app/src/app/secure/common-feed/common-feed.component.ts index a9d8fa0..d67cc50 100644 --- a/stackle-app/src/app/secure/common-feed/common-feed.component.ts +++ b/stackle-app/src/app/secure/common-feed/common-feed.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { PostService } from '../../services/post.service'; +import { UserService } from '../../services/user.service'; @Component({ selector: 'app-common-feed', @@ -15,7 +16,8 @@ export class CommonFeedComponent implements OnInit { constructor( private router: Router, - private postService: PostService + private postService: PostService, + private userService: UserService ) { } ngOnInit() { @@ -24,7 +26,7 @@ export class CommonFeedComponent implements OnInit { } private navigateToPost(post_id) { - this.router.navigate(['app/post/'+post_id]); + this.router.navigate(['app/post/' + post_id]); } getAllPosts() { @@ -38,9 +40,18 @@ export class CommonFeedComponent implements OnInit { } voteUp(id) { - this.postService.voteUp(id).subscribe( response => { - this.getAllPosts(); - }) + this.userService.getUser(localStorage.getItem('username')).subscribe((data) => { + //we have to push object id from document of this user so we have to pass that object id to backed + // we also have changed postService + this.postService.voteUp(id,data.result._id).subscribe(response => { + this.getAllPosts(); + }); + }, (err) => { + + }, () => { + console.log("Completed"); + }); + } } diff --git a/stackle-app/src/app/secure/shared/post/post.component.ts b/stackle-app/src/app/secure/shared/post/post.component.ts index cf63f82..0b8ab31 100644 --- a/stackle-app/src/app/secure/shared/post/post.component.ts +++ b/stackle-app/src/app/secure/shared/post/post.component.ts @@ -3,6 +3,7 @@ import { Router, ActivatedRoute, Params } from '@angular/router'; import { PostService } from '../../../services/post.service'; import { MatSnackBar } from '@angular/material'; import {ProfileService} from "../../../services/profile.service"; +import { UserService } from '../../../services/user.service'; @Component({ selector: 'app-post', @@ -21,7 +22,8 @@ export class PostComponent implements OnInit { private profileService: ProfileService, private router: Router, private activatedRoute: ActivatedRoute, - private snackBar: MatSnackBar + private snackBar: MatSnackBar, + private userService: UserService ) { } ngOnInit() { @@ -65,8 +67,14 @@ export class PostComponent implements OnInit { } voteOnPost(){ - this.postService.voteUp(this.postId).subscribe( response => { - this.getPostData(); + this.userService.getUser(localStorage.getItem('username')).subscribe((data) => { + this.postService.voteUp(this.postId,data.result._id).subscribe(response => { + this.getPostData(); + }); + }, (err) => { + + }, () => { + console.log("Completed"); }); } diff --git a/stackle-app/src/app/services/post.service.ts b/stackle-app/src/app/services/post.service.ts index c3c8ff4..a689670 100644 --- a/stackle-app/src/app/services/post.service.ts +++ b/stackle-app/src/app/services/post.service.ts @@ -31,8 +31,8 @@ export class PostService { return this.http.post(`${this.apiUrl}/api/post/create`, postObject); } - voteUp(id) { - return this.http.post(`${this.apiUrl}/api/post/likes/up/${id}`, {userId:localStorage.getItem('username')}); + voteUp(id,u_id) { + return this.http.post(`${this.apiUrl}/api/post/likes/up/${id}`, {userId:u_id}); } voteDown(id) { diff --git a/stackle_api/app/models/post.js b/stackle_api/app/models/post.js index 700a07f..0941017 100644 --- a/stackle_api/app/models/post.js +++ b/stackle_api/app/models/post.js @@ -606,7 +606,11 @@ postSchema.statics.setLikeUp = function(request, response) { var check = likeArray.indexOf(currentUserId); if (check === -1) { - result.likes.push(currentUserId); + // result.likes.push(currentUserId) + // here instead of pushing currentuserID which you are sending github username in string we have to push + // object id of that user from user schema that is why server carshes because you are pushing string to array + // of mongoose.Schema.Types.ObjectId + result.likes.push(mongoose.Types.ObjectId(currentUserId)); result.save(function(err) { if (err) return returnWithResponse.configureReturnData({ From bbca69b8db8f92a03018b270c3f3374012f51db8 Mon Sep 17 00:00:00 2001 From: vishwesh Date: Mon, 8 Apr 2019 00:20:22 +0530 Subject: [PATCH 2/2] Adding docker file --- stackle_api/config/database.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stackle_api/config/database.js b/stackle_api/config/database.js index aec56f0..21b1025 100644 --- a/stackle_api/config/database.js +++ b/stackle_api/config/database.js @@ -1,5 +1,6 @@ module.exports = { - url: process.env.LOCAL_DB || "mongodb://localhost/main", + // url: process.env.LOCAL_DB || "mongodb://localhost/main", + url: process.env.LOCAL_DB || "mongodb://mongo:27017/stackle", testurl: "mongodb://localhost/node-test", alturl: "mongodb://ordinary:H3ll0w0rld@ds149201.mlab.com:49201/stackle", option: function(version) {