MEAN Stack:post 方法未反映在数据库中
MEAN Stack: post method is not reflecting in database
我正在尝试通过 HTTP post 方法传递一些数据,但它没有反映在数据库中。
这是代码。
addJobList(jobitem) {
let headers = new Headers();
headers.append('Content-Type','application/json');
var selected = {
companyTitle : jobitem.company,
jobTitle : jobitem.jobtitle,
location : jobitem.location
}
console.log(selected);
this.http.post('http://localhost:3000/api/appliedjobs', JSON.stringify(selected),{headers: headers})
.map(res => res.json());
}
//getting jobs form back-end
getAppliedjobList() {
if (this.jobslist) {
return Promise.resolve(this.jobslist);
}
return new Promise( resolve => {
let headers = new Headers();
headers.append('Content-Type','application/json');
this.http.get('http://localhost:3000/api/appliedjobs',{headers: headers})
.map(res => res.json())
.subscribe(data => {
this.jobslist = data;
resolve(this.jobslist);
});
});
}
我在名为 selected 的对象中输入了数据。
{companyTitle: "Facebook", jobTitle: "System Engineer", location: "torinto,canada"}
来自控制台的数据。但是这些数据没有被插入到数据库中。
这是我的路线文件夹中的代码。
const jobList = require('../models/jobList');
router.post('/appliedjobs', function(req,res) {
console.log('posting');
jobList.create({
companyTitle: req.body.companyTitle,
jobTitle: req.body.jobTitle,
location: req.body.location
},function(err,list) {
if (err) {
console.log('err getting list '+ err);
} else {
res.json(list);
}
}
);
});
我没有收到任何错误,只是数据没有插入到数据库中。
这是我的模型
var mongoose = require('mongoose');
const joblistSchema = mongoose.Schema({
companyTitle: String,
jobTitle: String,
location: String,
});
const JlSchema = module.exports = mongoose.model('JlSchema',joblistSchema,'joblist');
您不需要像 example 中那样对您的数据进行编码,并且您必须 return 您的 this.http.post。
addJobList(jobitem) {
let headers = new Headers();
headers.append('Content-Type','application/json');
const selected = {
companyTitle : jobitem.company,
jobTitle : jobitem.jobtitle,
location : jobitem.location
}
return this.http.post('http://localhost:3000/api/appliedjobs', selected, { headers: headers })
.map(res => res.json());
}
要使用它,您需要订阅您的 addJobList 方法,http.post 是一个可观察对象,需要订阅才能进行 http 调用:
addJobList(theJobItem).subscribe(data => console.log(data));
我正在尝试通过 HTTP post 方法传递一些数据,但它没有反映在数据库中。 这是代码。
addJobList(jobitem) {
let headers = new Headers();
headers.append('Content-Type','application/json');
var selected = {
companyTitle : jobitem.company,
jobTitle : jobitem.jobtitle,
location : jobitem.location
}
console.log(selected);
this.http.post('http://localhost:3000/api/appliedjobs', JSON.stringify(selected),{headers: headers})
.map(res => res.json());
}
//getting jobs form back-end
getAppliedjobList() {
if (this.jobslist) {
return Promise.resolve(this.jobslist);
}
return new Promise( resolve => {
let headers = new Headers();
headers.append('Content-Type','application/json');
this.http.get('http://localhost:3000/api/appliedjobs',{headers: headers})
.map(res => res.json())
.subscribe(data => {
this.jobslist = data;
resolve(this.jobslist);
});
});
}
我在名为 selected 的对象中输入了数据。
{companyTitle: "Facebook", jobTitle: "System Engineer", location: "torinto,canada"}
来自控制台的数据。但是这些数据没有被插入到数据库中。 这是我的路线文件夹中的代码。
const jobList = require('../models/jobList');
router.post('/appliedjobs', function(req,res) {
console.log('posting');
jobList.create({
companyTitle: req.body.companyTitle,
jobTitle: req.body.jobTitle,
location: req.body.location
},function(err,list) {
if (err) {
console.log('err getting list '+ err);
} else {
res.json(list);
}
}
);
});
我没有收到任何错误,只是数据没有插入到数据库中。 这是我的模型
var mongoose = require('mongoose');
const joblistSchema = mongoose.Schema({
companyTitle: String,
jobTitle: String,
location: String,
});
const JlSchema = module.exports = mongoose.model('JlSchema',joblistSchema,'joblist');
您不需要像 example 中那样对您的数据进行编码,并且您必须 return 您的 this.http.post。
addJobList(jobitem) {
let headers = new Headers();
headers.append('Content-Type','application/json');
const selected = {
companyTitle : jobitem.company,
jobTitle : jobitem.jobtitle,
location : jobitem.location
}
return this.http.post('http://localhost:3000/api/appliedjobs', selected, { headers: headers })
.map(res => res.json());
}
要使用它,您需要订阅您的 addJobList 方法,http.post 是一个可观察对象,需要订阅才能进行 http 调用:
addJobList(theJobItem).subscribe(data => console.log(data));