Javascript 平均堆栈:无法将mongodb中的数据提取到前端;错误:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中
开发一个平均堆栈应用程序。使用角度6,节点-10.3.0。我试图从mongodb模型中获取数据,并将该数据显示到HTML页面。但我认为我犯了一个小错误。试图搜索相同的,但无法解决错误 错误为Javascript 平均堆栈:无法将mongodb中的数据提取到前端;错误:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中,javascript,html,node.js,angular,mongodb,Javascript,Html,Node.js,Angular,Mongodb,开发一个平均堆栈应用程序。使用角度6,节点-10.3.0。我试图从mongodb模型中获取数据,并将该数据显示到HTML页面。但我认为我犯了一个小错误。试图搜索相同的,但无法解决错误 错误为 SyntaxError: clients.component.ts Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at Response.push../node_modules
SyntaxError: clients.component.ts
Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Response.push../node_modules/@angular/http/fesm5/http.js.Body.json
这是客户端组件文件
getClients() {
let headers = new Headers ({ 'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
return this._http.get('http://localhost:3000/admin/clients', options).pipe(
map((response: Response) => response));
}
export class ClientsComponent implements OnInit {
clients: any[];
constructor( private clientService: ClientService ) { }
ngOnInit() {
this.getAllClients();
}
getAllClients(){
this.clientService.getClients().subscribe((res) => {
console.log(res);
this.clients = res;
}, err => {
console.log(err);
});
}
}
这是html文件
<table>
<tr>
<th>S.No.</th>
<th>Client Name</th>
<th>Client Phone No</th>
<th>Client Address</th>
</tr>
<tr *ngFor="let client of clients">
<th>1</th>
<th>{{ client.clientName}}</th>
<th>{{ client.clientPhoneNo }}</th>
<th>{{ client.clientAddress }}</th>
</tr>
</table>
var client = require('./server/routes/clients');
app.use('/api', client.router);
app.use(express.static(path.join(__dirname + '/dist/mean-travels')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, '/dist/mean-travels/index.html'));
});
app.post('/admin/clients/add', client.saveclient);
app.get('/admin/clients', client.getclient);
下面是获取客户端路由函数
<table>
<tr>
<th>S.No.</th>
<th>Client Name</th>
<th>Client Phone No</th>
<th>Client Address</th>
</tr>
<tr *ngFor="let client of clients">
<th>1</th>
<th>{{ client.clientName}}</th>
<th>{{ client.clientPhoneNo }}</th>
<th>{{ client.clientAddress }}</th>
</tr>
</table>
var client = require('./server/routes/clients');
app.use('/api', client.router);
app.use(express.static(path.join(__dirname + '/dist/mean-travels')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, '/dist/mean-travels/index.html'));
});
app.post('/admin/clients/add', client.saveclient);
app.get('/admin/clients', client.getclient);
函数getclient(req、res、next){
console.log(“查找所有客户端”);
find({},函数(err,clients){
if(err)返回next(err);
console.log(客户端);
res.json(客户);
});
};
exports={getclient}
看起来像是返回HTML,而不是JSON。因此,您的JSON解析当然会失败。@sneha_h您是否能够控制台res
如果是,那么控制台的输出是什么?MongoDB查询可能有问题或与此相关@是的,没错!它可能是:>您认为应该是JSON的内容,以一个“