Angular 从express向mongoDB发送请求
我正试图通过Express将信息发布到mongoDB。所有到DB的连接都正常工作,因为get请求工作正常 在我的server.js中,我定义了模式Angular 从express向mongoDB发送请求,angular,mongodb,express,Angular,Mongodb,Express,我正试图通过Express将信息发布到mongoDB。所有到DB的连接都正常工作,因为get请求工作正常 在我的server.js中,我定义了模式 var todoSchema = new Schema({ taskName: String, createdAt: Date, isDone: Boolean, prioraty: String }, { collection: 'tasks' }); var Model = mongoose.mo
var todoSchema = new Schema({
taskName: String,
createdAt: Date,
isDone: Boolean,
prioraty: String
}, {
collection: 'tasks'
});
var Model = mongoose.model('Model', todoSchema);
然后是app.post请求
app.post('/tasks', function(req, res) {
var savedata = new Model({
'taskName': req,
'isDone': false,
'createdAt': Date.now(),
'prioraty': 'medium'
}).save(function (err, result) {
if (err) throw err;
if (result) {
res.json(result)
}
})
});
这根本不起作用
在我的前端,我通过按下按钮来调用它,但什么也没发生
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
我怎样才能修好它?如前所述,GET请求工作正常,所以我希望我走的是正确的道路
下面是get请求的例子
app.get('/tasks', (req, res) => {
Model.find({
// 'request': query
}, function(err, result) {
if (err) throw err;
if (result) {
res.json(result)
} else {
res.send(JSON.stringify({
error : 'Error'
}))
}
})
});
查看您的
POST
,taskName
是一个基于模式的字符串,但您正在向它传递完整的req
对象。如果您在请求中传递的是req.body
中的属性,那么它应该是req.body.taskName
中的属性。查看您的POST
,taskName
是一个基于模式的字符串,但您正在传递完整的req
对象。它应该是req.body
中的属性类似于req.body.taskName
,如果这是您在请求中传递的内容。首先,您需要在前端接收响应
在用:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
如果您使用的是angularjs
(angular1+)
您需要使用然后
和捕获
来接收响应
在控制器中:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
如果您使用的是angular
(angular 2+)
您需要使用subscribe
作为this。http
返回observeable
组件中:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
第二,正如@andrew提到的
taskName
您应该发送string
,因为您有taskName
模式作为string
var todoSchema = new Schema({
taskName: String,
})
更新:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
req.body包含请求中提交的数据的键值对
身体。默认情况下,它是未定义的,并且在使用时填充
主体解析中间件,如主体解析器
下面的示例显示如何使用正文解析中间件
来填充req.body
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/tasks', function(req, res) {
console.log(req.body)
});
首先,您需要在前端接收您的回复 在用:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
如果您使用的是angularjs
(angular1+)
您需要使用然后
和捕获
来接收响应
在控制器中:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
如果您使用的是angular
(angular 2+)
您需要使用subscribe
作为this。http
返回observeable
组件中:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
第二,正如@andrew提到的
taskName
您应该发送string
,因为您有taskName
模式作为string
var todoSchema = new Schema({
taskName: String,
})
更新:
postTask(task) {
return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
console.log(response)
}).catch(function(error) {
console.log(error)
})
this.service.postTask(task).subscribe( response => {
console.log(response)
})
req.body包含请求中提交的数据的键值对
身体。默认情况下,它是未定义的,并且在使用时填充
主体解析中间件,如主体解析器
下面的示例显示如何使用正文解析中间件
来填充req.body
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/tasks', function(req, res) {
console.log(req.body)
});
您是否
订阅了组件中的您使用的是哪个版本的angular
?您应该订阅您的post请求
为post
请求添加错误消息,以及您在post请求中发送的任务
对象的示例,这很有帮助。您是否在组件中订阅您正在使用的angular
的哪个版本?您应该订阅您的post请求
为post
请求添加错误消息,以及您在post请求中发送的任务
对象的示例,这会很有帮助。谢谢!这帮了大忙,还有一个问题——我的请求主体现在是空的……那么我该如何解决这个问题呢?”taskName':req,因此,例如,如果我写'taskName':“test3”,它工作正常,但我无法从functionyou consolereq.body
,如果console.log(req),请尝试req.body.taskName
或req.params
您将了解存在的内容您需要使用bodyParser
获取req.body
请检查更新的答案。你需要npm安装body解析器--save
我已经在使用它了。。。如果我尝试控制台req.body,我的console.log会给我空对象{},谢谢!这帮了大忙,还有一个问题——我的请求主体现在是空的……那么我该如何解决这个问题呢?”taskName':req,因此,例如,如果我写'taskName':“test3”,它工作正常,但我无法从functionyou consolereq.body
,如果console.log(req),请尝试req.body.taskName
或req.params
您将了解存在的内容您需要使用bodyParser
获取req.body
请检查更新的答案。你需要npm安装body解析器--save
我已经在使用它了。。。如果我尝试控制台req.body,我的console.log会给我空对象{}