MEAN Stack:post方法没有反映在数据库中 [英] MEAN Stack: post method is not reflecting in database

查看:110
本文介绍了MEAN Stack:post方法没有反映在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过HTTP post方法传递一些数据,但它没有反映在数据库中。
这是代码。

I'm trying to pass some data through HTTP post method but it is not reflecting in the database. This is code.

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的对象中输入数据。

I've data in in the object called selected.

{companyTitle: "Facebook", jobTitle: "System Engineer", location: "torinto,canada"}

来自控制台的数据。但是这些数据没有插入到数据库中。
这是我的路线文件夹中的代码。

data from console. But this data is not get inserted into the database. This is the code in my routes folder.

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);
    }
  }
  );
});

我没有收到任何错误,只是数据没有插入数据库。
这是我的模特

I'm not getting any error just the data is not getting inserted into the database. This is my model

var mongoose = require('mongoose');

const joblistSchema = mongoose.Schema({
    companyTitle: String,
    jobTitle: String,
    location: String,
});

const JlSchema = module.exports = mongoose.model('JlSchema',joblistSchema,'joblist');


推荐答案

您无需对数据进行编码这个示例,您必须返回您的this.http。 post。

You don't need to encode your data like in this example and you must return your 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调用:

To use it you need to subscribe to your addJobList method, http.post is an observable and needs to be subscribed to make the http call :

addJobList(theJobItem).subscribe(data => console.log(data));

这篇关于MEAN Stack:post方法没有反映在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆