[英]Get request is not fulfilling Angular-Express
I'm new to MEAN stack I want to have data fetch from express server (which I created with mongodb) when I click button, the function on button click was executed but request was not fulfilled, the database, server all running fine, I just got nothing at console, here are my client and server files: 我是MEAN堆栈的新手,我想在单击按钮时从Express服务器(我使用mongodb创建)中获取数据,执行了按钮单击的功能,但请求未得到满足,数据库,服务器都运行良好,我在控制台上什么都没有,这是我的客户端和服务器文件:
client(running on http://localhost:3001 ) 客户端(在http:// localhost:3001上运行 )
Imported modules and added to imports in ngModule 导入的模块并添加到ngModule中的导入中
import { Http , Headers, HttpModule, Response, RequestOptions } from "@angular/http";
从“ @ angular / http”导入{Http,标头,HttpModule,Response,RequestOptions};
Injected in constructor as service, then: template: 作为服务注入构造函数,然后:template:
<button md-button class="button1" (click)="newQues()">Submit</button>
In class: 在班上:
newQues(){
this.http.get('https://localhost:3000/')
.map(this.extractData)
.catch(this.handleError); //these copied from angular2
//official documentation
}
private extractData(res: Response) {
let body = res.json();
console.log(body); //not getting console nor error
return body.data || { };
}
server running on port 3000 在端口3000上运行的服务器
app.get('/', function (req, res) {
model.findOne({ id: 1}, function (err, data) {
if (err) {
console.log(err)
} else {
console.log(data);
res.send(data);
}
})
getting these error when requesting 请求时出现这些错误
the whole server code: 整个服务器代码:
import * as express from "express";
var app = express();
import * as path from "path";
import * as mongoose from "mongoose";
import * as lodash from "lodash";
import * as morgan from "morgan";
app.use(express.static(path.join(__dirname, "./../client")));
var uriString = 'mongodb://localhost/quizapp';
mongoose.connect(uriString, function (err, res) {
if (err) {
console.log("Error occured while establishing connection " + err);
} else {
console.log("Connection successful ");
}
})
var schema = new mongoose.Schema({ //schema
question: String,
id: Number
})
var quizz = mongoose.model('Quiz', schema); //creating model
var firstDoc = new quizz({
question: 'question 1',
id: 1
})
var secondDoc = new quizz({
question: 'question 2',
id: 2
})
var question_data = [firstDoc, secondDoc];
quizz.insertMany(question_data, function (err, res) {
if (err) {
console.log("Error occured while saving document object " + err)
} else {
console.log("Data Saved Successful");
}
})
app.get('/data', function(req, res) {
quizz.findOne({ id: 1}, function (err, data) {
if (err) {
console.log(err)
} else {
console.log(data);
res.send(data); //res.json
}
})
})
when I'm routing to localhost:3000/data the data from database displayed on window but when requesting that route from client(see client side code this.http.get) error occurring(picture attached) 当我路由到localhost:3000 / data时,窗口中显示的数据库中的数据却在从客户端请求该路由时(请参阅客户端代码this.http.get)发生错误(附图片)
for extracting data you have to subscribe to it. 要提取数据,您必须订阅它。 try this way
尝试这种方式
export class SimpleHTTPComponent {
data: Object;
loading: boolean;
constructor(public http: Http) {
}
makeRequest(): void {
this.loading = true;
this.http.request('http://jsonplaceholder.typicode.com/posts/1')
.subscribe((res: Response) => {
this.data = res.json();
this.loading = false;
});
}
}
and i think you are not using this
with body
. 而且我认为你不会在
body
使用this
。 your console.log should be like this. 您的console.log应该是这样的。
console.log(this.body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.