fetching details from API and trying to list the same getting error while printing the list of users was able print with same code.
following is my home.component.ts
constructor(private formBuilder: FormBuilder, private data: DataService) { }
ngOnInit() {
this.data.getUsers().subscribe(data => {
this.users = data
console.log(this.users);
}
);
}
following is my data services code
getUsers() {
return this.http.get('https://reqres.in/api/users');
}
following is the html loop I am trying
<ul *ngIf="users">
<li *ngFor="let user of users.data">
<img [src]="user.avatar">
<p>{{ user.first_name }} {{ user.last_name }}</p>
</li>
</ul>
Re-Check this from your code Working Stackblitz
component.ts
import { Component, OnInit } from '#angular/core';
import { DataService } from './data.service';
#Component({
selector: 'my-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
users;
constructor(private data: DataService){}
ngOnInit() {
this.data.getUsers().subscribe(data => {
this.users = data ;
})
}
}
service.ts
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
#Injectable()
export class DataService {
constructor(private http: HttpClient) { }
getUsers() {
return this.http.get('https://reqres.in/api/users');
}
}
component.html
<ul *ngIf="users">
<li *ngFor="let user of users.data">
<img [src]="user.avatar">
<p>{{ user.first_name }} {{ user.last_name }}</p>
</li>
</ul>
Worked... I cleared cache of npm and it worked ..
Related
I am trying to add a json file formatted to fit the formlyconfig file but unable to do so
Here is my HTML
<div class="row">
<div class="col-md-4 mb-4">
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<formly-form [form]="form"
[model]="model"
[fields]="fields">
</formly-form>
</form>
</div>
</div>
Here is my TS file
import { Component, OnInit } from '#angular/core';
import { FormGroup } from '#angular/forms';
import { ActivatedRoute, Router } from '#angular/router';
import { FormlyFieldConfig } from '#ngx-formly/core';
import { CheckListClient, CheckListDTO } from '../../web-api-client';
#Component({
selector: 'app-service-form-formly',
templateUrl: './service-form-formly.component.html',
styleUrls: ['./service-form-formly.component.css']
})
export class ServiceFormFormlyComponent implements OnInit {
checklistDtos: CheckListDTO[];
isEditable: boolean = true;
constructor(private clientChecklist: CheckListClient, private router: Router, private route: ActivatedRoute) { }
ngOnInit(): void {
this.filterChecklist();
}
filterChecklist() {
this.clientChecklist.getAllCheckList().subscribe(result => {
this.checklistDtos = result;
});
}
form = new FormGroup({});
model = {};
fields: FormlyFieldConfig[] = [
];
onSubmit() {
if (this.form.valid) {
alert(JSON.stringify(this.model, null, 2));
}
}
}
the JSON value is inside this.checklistDtos = result;
Please help me out with this
You have to put the result in fields, like
this.fields = result or
this.fields = this.checklistDtos
I'm building a blog application based on MEAN stack, using Angular in front-end, node in back-end and mongoDB for server. When I'm trying to access a particular blog by it's blogId, the browser is not showing anything. Although I'm getting the right data fetched from backend with a 304 status and the right blogId is also passing by the route. The console is also logging the right object. Below is my blog-view.component.ts file
import { ActivatedRoute, Router } from '#angular/router';
import { ToastrManager } from 'ng6-toastr-notifications';
import { BlogService } from '../blog.service';
import { BlogHttpService } from '../blog-http.service';
import { Location } from '#angular/common';
#Component({
selector: 'app-blog-view',
templateUrl: './blog-view.component.html',
styleUrls: ['./blog-view.component.css'],
providers: [Location]
})
export class BlogViewComponent implements OnInit, OnDestroy {
public currentBlog;
constructor(private _route: ActivatedRoute, private router: Router, public blogHttpService:BlogHttpService, public toastr: ToastrManager, private location: Location) {
console.log("view-blog constructor called")
}
ngOnInit() {
console.log("view-blog ngOnInIt called");
let myBlogId = this._route.snapshot.paramMap.get('blogId');
console.log(myBlogId);
this.currentBlog = this.blogHttpService.getSingleBlogInformation(myBlogId).subscribe(
data =>{
console.log(data);
this.currentBlog = data["data"];
},
error =>{
console.log("some error occured");
console.log(error.errorMessage);
})
}
public deleteThisBlog(): any {
this.blogHttpService.deleteBlog(this.currentBlog.blogId).subscribe(
data =>{
console.log(data);
this.toastr.successToastr('This blog is successfully deleted.', 'Success!');
setTimeout(() =>{
this.router.navigate(['/home']);
}, 1000)
},
error =>{
console.log(error);
console.log(error.errorMessage);
this.toastr.errorToastr('Some Error Occured.', 'Oops!');
}
)
}
public goBackToPreviousPage(): any {
this.location.back();
}
ngOnDestroy(){
console.log("view-blog component destroyed");
}
}
blog-view.component.html
<div class="row" *ngIf="currentBlog" style="text-align: center;">
<div class="col-md-12">
<h2>{{currentBlog.title}}</h2>
<p>posted by {{currentBlog.author}} on {{currentBlog.created | date:'medium'}}</p>
<p *ngIf="currentBlog.tags!=undefined && currentBlog.tags.length>0">tags : <span *ngFor="let tag of currentBlog.tags;let first=first;let last=last">{{tag}}{{last ? '' : ', '}}</span></p>
<hr>
<div [innerHtml]="currentBlog.bodyHtml"></div>
<hr>
<h5>category - {{currentBlog.category}}</h5>
</div>
<hr>
<div class="row" *ngIf="currentBlog">
<div class="col-md-4">
<a class="btn btn-primary" [routerLink]="['/edit',currentBlog.blogId]">Edit</a>
</div>
<div class="col-md-4">
<a class="btn btn-danger" (click)="deleteThisBlog()">Delete</a>
</div>
<div class="col-md-4">
<a class="btn btn-warning" (click)="goBackToPreviousPage()">Go Back</a>
</div>
</div>
</div>
</div>
blog-http.service.ts
import { HttpClient, HttpErrorResponse } from '#angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
#Injectable()
export class BlogHttpService {
public allBlogs;
public currentBlog;
public baseUrl = 'http://localhost:3000/api/v1/blogs';
constructor(private _http:HttpClient) {
console.log('blog http service constructor called');
}
private handleError(err:HttpErrorResponse){
console.log("handle error http calls");
console.log(err.message);
return Observable.throw(err.message)
}
public getAllBlogs(): any {
let myResponse = this._http.get(this.baseUrl + '/all');
console.log(myResponse);
return myResponse;
}
public getSingleBlogInformation(currentBlogId): any {
let myResponse = this._http.get(this.baseUrl + '/view/' + currentBlogId);
return myResponse;
}
public createBlog(blogData): any {
let myResponse = this._http.post(this.baseUrl + '/create', blogData);
return myResponse;
}
public deleteBlog(blogId): any {
let data = {}
let myResponse = this._http.post(this.baseUrl + '/' + blogId + '/delete', blogId);
return myResponse;
}
public editBlog(blogId, blogData): any {
let myResponse = this._http.put(this.baseUrl + '/' + blogId + '/edit' , blogData);
return myResponse;
}
}
my nodejs routes with controller for blog-view
app.get(baseUrl+'/view/:blogId', (req, res) => {
BlogModel.findOne({ 'blogId': req.params.blogId }, (err, result) => {
if (err) {
console.log(err)
res.send(err)
} else if (result == undefined || result == null || result == '') {
console.log('No Blog Found')
res.send("No Blog Found")
} else {
res.send(result)
}
})
});
Below is a sample document object from which the frontend should render
{
"_id": "5e0e8ac6dcfc4e2008390cdf",
"blogId": "XAY2Qlhb",
"__v": 0,
"lastModified": "2020-01-03T00:28:54.638Z",
"created": "2020-01-03T00:28:54.638Z",
"tags": [
"english movies, action movies"
],
"author": "Decardo",
"category": "Hollywood custom",
"isPublished": true,
"views": 8,
"bodyHtml": "<h1>Heading of the body</h1>\n<p>This is the first blog data getting uploaded n blog project</p>",
"description": "long description>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",
"title": "Blog Title 1 custom"
}
This line of code will first store a Subscription in this.currentBlog:
this.currentBlog = this.blogHttpService.getSingleBlogInformation(myBlogId).subscribe(...)
Then the subscription will be overwritten by this.currentBlog = data["data"];
this.currentBlog = is actually not needed there.
Not sure if that is the problem. At least it is not proper :)
I am currently learning node.js and creating test application using angular so in that i need to retrieve data stored in Mongodb below i have written query to get mongodb data using node.js but unable to get data so what is the exact procedure for changes need to be done in query.
In Html(component in angular):
<div class="col-md-12">
<div class="container">
<table class="table mar-top-20">
<thead class="thead-dark">
<tr>
<th>Date</th>
<th>Client Name</th>
<th>Client Email</th>
<th>Client Password</th>
<th>Action/Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{userDetails.added_on}}</td>
<td>{{userDetails.username}}</td>
<td>{{userDetails.uemail}}</td>
<td>{{userDetails.upassword}}</td>
<td>Show Detail</td>
</tr>
</tbody>
</table>
</div>
</div>
In TS file(component in angular):
import { Component, OnInit } from '#angular/core';
import { FormBuilder, FormGroup , Validators } from '#angular/forms';
import { DataService } from '../../../services/data.service';
import { AccountService } from '../../../services/account.service';
import { Router } from '#angular/router';
// import { Client } from '../../../client';
#Component({
selector: 'app-manage-users',
templateUrl: './manage-users.component.html',
styleUrls: ['./manage-users.component.css'],
})
export class ManageUsersComponent implements OnInit {
userDetails: any;
rForm: FormGroup;
post:any;
username: String = '';
uemail: String = '';
upassword: String = '';
constructor(private router: Router,private fb:FormBuilder,private dataService: DataService, private accountService: AccountService) {
this.rForm = fb.group({
'username':['', Validators.required],
'uemail':['', Validators.required],
'upassword': [ '', Validators.required ]
});
}
ngOnInit() {
//Getting user using userId
this.accountService.getUser({ userId: localStorage.getItem('userId') }).subscribe(
response => {
console.log(response);
this.userDetails = JSON.parse(response);
},
err => {
console.error('User Not found');
})
}
}
accountService.ts:
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
import { DataService } from './data.service';
#Injectable()
export class AccountService {
public apiPath: string;//Api URL where we need to send our data & perform related operations
constructor(ds: DataService, private http: HttpClient) {
this.apiPath = ds.apiPath;
}
getUser(user) {//Getting User with userId
return this.http.post(this.apiPath + 'user/getUser', user, { responseType: 'text' });
}
}
UserController.js:
userRouter.post('/getUser', (req, res) => {
var user = {
_id: req.body.userId,
};
Collections.user.findOne(user, function (err, result) {
if (err) return res.status(500).send("There was a problem finding the user");
if (result) {
res.status(200).send(result);
} else {
res.status(500).send("User Not Found with Details: " + JSON.stringify(user));
}
});
});
My database name is testdb and collection name is users
I've imported the required modules also checked for errors but nothing helps.
I am new to angular and still learning.
Here's is my app.module.ts file
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { CommonModule } from "#angular/common";
import { AppComponent } from './app.component';
import { ContactsComponent } from './contacts/contacts.component';
#NgModule({
declarations: [
AppComponent,
ContactsComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
CommonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
And here's the part of the code which is not working, but not working throwing any error either.
<div class="container">
<div *ngFor = "let contact of contacts" >
<div class = "col-md-3">
{{contact.first_name}}
</div>
<div class = "col-md-3">
{{contact.last_name}}
</div>
<div class = "col-md-3">
{{contact.phone}}
</div>
<div class = "col-md-3">
<input type="button" (click)="deleteContact(contact._id)" value ="Delete" class="btn btn-danger" />
</div>
</div>
</div>
Update: I've included my contact.srevice.ts file.
import { Injectable } from '#angular/core';
import {Http, Headers} from '#angular/http';
import {Contact} from './contact';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
#Injectable()
export class ContactService {
constructor(private http: Http) { }
//retrieving contact_service
getContacts() :Observable<any[]>
{
return this.http.get('http://localhost:3000/api/contacts')
.map(res => res.json());
}
//add contact method
addContact(newContact){
var headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:3000/api/contact', newContact, {headers:headers})
.map(res=>res.json());
}
//delete Contact Method
deleteContact(id){
return this.http.delete('http://localhost:3000/api/contact/'+id)
.map(res=>res.json());
}
}
Network Activity
As per your .ts code you need to bind some data with your object named as contacts like this to get it work
contacts = [
{first_name: "Pardeep", last_name: "Jain", phone: '90'},
{first_name: "Pardeep", last_name: "Jain", phone: '90'},
{first_name: "Pardeep", last_name: "Jain", phone: '90'}
]
Working Example
Update
You need to bind data whatever you get from this service like this -
ngOnInit() {
this.service.getContacts()
.subscribe(data=> this.contacts = data)
}
you need to do like this
import { Component } from '#angular/core';
import {Contact} from './contact';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'] })
export class AppComponent implements OnInit
{
title = 'New App';
contacts: Contact[];
ngOnInit() {
this.service.getContacts()
.subscribe(data=> this.contacts = data)
}
}
contact.service.ts file
//make use of newer api of http
import { HttpClient, HttpHeaders } from '#angular/common/http';
export class ContactService {
constructor(private http: HttpClient) { }
getContacts() :Observable<Contact[]>
{
return
this.http.get<Contact[]>('http://localhost:3000/api/contacts');
}
addContact(newContact) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
return this.http.post('http://localhost:3000/api/contact', newContact, httpOptions)
.map(res=>res.json());
}
}
My objective is when the user clicks a message displayed on /messages, it redirects them to /message with the messages displayed information. Please direct me in the right direction if I'm doing something wrong.
This is my error:
Unhandled Promise rejection: Template parse errors:
Can't bind to 'messages' since it isn't a known property of 'message'. ("ge-header">{{user.name}}</h2>
<ul class="list-group" style="list-style-type: none;">
<message [ERROR ->][messages]="message"></message>
Inbox: <ul class="list-group" style="list-style-type: none;">
"): MessagesComponent#3:13
'message' is not a known element:
1. If 'message' is an Angular component, then verify that it is part of this module.
2. If 'message' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '#NgModule.schemas' of this component to suppress this message. ("class="page-header">{{user.name}}</h2>
<ul class="list-group" style="list-style-type: none;">
[ERROR ->]<message [messages]="message"></message>
Inbox: <ul class="list-group" style="list-style-type: no"): MessagesComponent#3:4 ; Zone: <root> ; Task: Promise.then ; Value:
Messages Component:
import { Component, OnInit } from '#angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '#angular/router';
#Component({
selector: 'app-messages',
templateUrl: './messages.component.html',
styleUrls: ['./messages.component.css']
})
export class MessagesComponent implements OnInit {
messages: Object;
username: String;
user: Object;
messageID: String;
public UserList: Object;
public RecpList: Object;
constructor(private authService: AuthService,
private router: Router
) { }
ngOnInit() {
this.authService.getProfile().subscribe(profile => {
this.user = profile.user;
this.username = profile.user.username;
},
err => {
console.log(err);
return false;
});
this.authService.getMessages(this.username).subscribe(list => {
this.UserList = list.UserList;
this.RecpList = list.RecpList;
},
err => {
console.log(err);
return false;
});
}
onMessageClick(messageID){
this.authService.getMessage(this.user, messageID).subscribe(list => {
console.log(list);
this.router.navigate(['/message', ]);
});
}
}
Messages html:
<div *ngIf="user">
<h2 class="page-header">{{user.name}}</h2>
<ul class="list-group" style="list-style-type: none;">
<message [messages]="message"></message>
Inbox: <ul class="list-group" style="list-style-type: none;">
<li *ngFor="let message of RecpList" (click)="onMessageClick(message.messageID)" class="list-group" style="text-align: center;"><strong>{{message.createdBy}}</strong> {{message.subject}} <strong>{{message.dateCreated}}</strong></li>
</ul>
Sent: <ul class="list-group" style="list-style-type: none;">
<li *ngFor="let message of UserList" (click)="onMessageClick(message.messageID)" class="list-group" style="text-align: center;"><strong>{{message.recipients}}</strong> {{message.subject}} <strong>{{message.dateCreated}}</strong></li>
</ul>
<li class="list-group"><a [routerLink] = "['/newmessage']">New Message</a></li>
</ul>
</div>
Message Component:
import { Component, OnInit } from '#angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '#angular/router';
#Component({
selector: 'app-message',
templateUrl: './message.component.html',
styleUrls: ['./message.component.css'],
inputs: ['messages']
})
export class MessageComponent implements OnInit {
messages: Object;
user: Object;
createdBy: String;
recipients: String;
subject: String;
message: String;
previousMessage: String;
nextMessage: String;
constructor(private authService: AuthService,
private router: Router) { }
ngOnInit() {
this.authService.getProfile().subscribe(profile => {
this.user = profile.user;
},
err => {
console.log(err);
return false;
});
}
}
Message html:
<p>{{messages.subject}}</p>
This is my component structure
So it looks like a couple of things:
You need to include the id or message in here this.router.navigate(['/message', ]); which is looks like you may have missed adding it in there
You need to specify the path in your router like this { path: 'message/:id', component: MessageComponent }
You can retrieve what is passed in by getting it from the params using something like this:
this.message = this.route.params.subscribe(params => {
this.id = params['id'];
});