In the book, it is not stated how we can manually create a new
location.
Now, am creating a forum app(just for testing) where before users can comment, a user must start a thread.
Am having trouble creating it the script but this is have been able to do
I have these codes in
posts.js
file which is located in
app_api/controllers folder
const getAuthor = (req, res, callback) => {
if (req.payload && req.payload.email) {
User
.findOne({ email: req.payload.email })
.exec((err, user) => {
if (!user) {
return res
.status(404)
.json({ "message": "User not found" });
} else if (err) {
console.log(err);
return res
.status(404)
.json(err);
}
callback(req, res, user.last_name);
});
} else {
return res
.status(404)
.json({ "message": "User not found" });
}
};
const doAddPost = (req, res, post, author) => {
const { postText, title } = req.body;
post.posts.push({
author,
postText,
title
});
post.save((err, post) => {
if (err) {
res
.status(400)
.json(err);
} else {
//updateAverageRating(post._id);
const thisPost = post.posts.slice(-1).pop();
res
.status(201)
.json(thisPost);
}
});
};
const postsCreate = (req, res) => {
getAuthor(req, res,
(req, res, username) => {
if (req) {
doAddPost(req, res, post);
} else {
res
.status(404)
.json({ "message": "Post not created" });
}
});
};
Then in index.js file of app_api/route folder, I have this
router.post('/post/create', auth, ctrlPosts.postsList);
Meanwhile, in app_public folder(which as you know hold the angular side).
These codes reside in the homelist.component.ts
@Input() post: Post;
public newPost: Post = {
postText: '',
user: '',
title: ''
};
public formVisible: boolean = false;
public formError: string;
private formIsValid(): boolean {
if (this.newPost.postText && this.newPost.title) {
return true;
} else {
return false;
}
}
`// The error is inside the promise here`
public onPostSubmit(): void {
this.formError = '';
this.newPost.user = this.getUsername();
if (this.formIsValid()) {
this.dayoDataService.addPost(this.post, this.newPost)
.then((post: Post) => {
console.log('Post saved', post);
let posts = this.post.slice(0);
posts.unshift(post);
this.post = posts;
this.resetAndHidePostForm();
this.router.navigateByUrl('/');
});
} else {
this.formError = 'All fields requried, please try again';
}
}
private resetAndHidePostForm(): void {
this.formVisible = false;
//this.newPost.author = '';
//this.newPost.rating = 5;
this.newPost.postText = '';
this.newPost.title = '';
}
public isLoggedIn(): boolean {
return this.authenticationService.isLoggedIn();
}
public getUsername(): string {
const { last_name } = this.authenticationService.getCurrentUser();
return last_name ? last_name : 'Guest';
}
Note: I only posted the neccessary code here as I believe you will understand
while I have this in
homelist.component.html
<div class="row">
<div class="col-12">
<div class="card card-primary review-card">
<div class="card-block" [ngSwitch]="isLoggedIn()">
<button (click)="formVisible=true" class="btn btn-primary float-right" *ngSwitchCase="true">Add post</button>
<a routerLink="/login" class="btn btn-primary float-right" *ngSwitchDefault>Log in to add post</a>
<div *ngIf="formVisible">
<form (ngSubmit)="onPostSubmit()">
<hr>
<h4>Start a new thread</h4>
<div *ngIf="formError" class="alert alert-danger" role="alert">
{{ formError }}
</div>
<div class="form-group row">
<label for="name">Title</label>
<input class="form-control" id="title" name="title" placeholder="Enter your first name" [(ngModel)]="newPost.title">
</div>
<div class="form-group row">
<label for="review" class="col-sm-2 col-form-label">Post</label>
<div class="col-sm-10">
<textarea [(ngModel)]="newPost.postText" name="post" id="post" rows="5" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<button type="submit" class="btn btn-primary float-right" style="margin-left:15px">Submit post</button>
<button (click)="formVisible=false" type="button" class="btn btn-default float-right">Cancel</button>
</div>
</div>
<hr>
</form>
</div>
</div>
</div>
</div>
</div>
This is what I have so far but am loss on how to complete it.
I would be grateful if you can help me out.
Also, I eagerly await your response