使用保存功能使用post方法插入数据,在同一个方法中需要获取相同的json数据,我们用id插入文件
apiRoutes.post('/doctor', function(req, res){
if(!req.body.Name || !req.body.password){
res.json({success: false, msg: 'please pass the username and password'});
}else{
var newUser = new Doctor({
Name:req.body.Name,
password : req.body.password,
});
newUser.save(function(err){
if(err){
res.json({success: false, msg :'username alredy existes'});
}else{
res.json({success: true, msg : 'Successfull created user'});
}
});
}
});
在res.json中需要使用documnet的_id返回相同的文档名称和密码
答案 0 :(得分:2)
根据您的要求,您希望通过POST方法在db中输入名称和密码。然后你就可以这么简单了。
apiRoutes.post('/doctor', function (req, res) {
var newUser = req.Collection;
var name = req.body.Name;
var password = req.body.password;
var record = new newUser({
name: name,
password: password,
});
if (name && password) {
record.save(function (err, result) {
if (err) {
res.json({status: 0, message:" username alredy existes"})
} else {
res.json({status: 1, name: name, password: password, message: " Successfull created user"});
}
})
} else {
res.json({status: 0, msg: "Invalid Fields"});
}
});
答案 1 :(得分:0)
我认为您可以使用.get()
方法将/path/:id
作为第一个参数。像这样的东西:
apiRoutes.get('/doctor/:id', function(req, res){
// your code goes here
});
因此,从客户端,您可以使用类似/doctor/65431
(id)
有关快递.get方法here
的更多信息答案 2 :(得分:0)
试试这个
apiRoutes.post('/doctor', function(req, res){
if(!req.body.Name || !req.body.password){
res.json({success: false, msg: 'please pass the username and password'});
}else{
var newUser = new Doctor({
Name:req.body.Name,
password : req.body.password,
});
newUser.save(function(err){
if(err){
res.send({'success': 'false', 'msg' :'username alredy existes'});
}else{
res.send({'success': 'true', 'msg' : 'Successfull created user','data':newUser});
}
});
}
});