我的req.body给了我null值。我如何获取值以便我可以将它存储在db.So中,当我刷新数据时数据会消失,因为db中没有字段。< / p>
.js file
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var morgan = require('morgan');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var cors = require('cors');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost/contactlist');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type:'application/vnd.api+json'}));
app.use(methodOverride());
app.use(cors());
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Methods", "DELETE,PUT");
res.header("Access-Control-Allow-Headers", "Origin,X-Requested-With, Content-Type, Accept");
next();
});
var contactSchema = mongoose.Schema({
name:String,
email:String,
number:Number,
address:String
});
var Contact = mongoose.model("Contact", contactSchema);
//Routers
//get
app.get('/contacts',function(req,res){
console.log('inside get router fetching the contacts');
Contact.find(function(err, contacts){
if(err)
res.send(err);
res.json(contacts);
});
});
//post---->get
app.post('/contacts',function(req,res){
console.log('creating the contacts');
if(!req.body.name || !req.body.email || !req.body.number || !req.body.address){
// res.render('show_message', {message: "Sorry, you provided worng info"});
console.log('information not provided');
console.log(req.body.name); //undefined
console.log(req.body.email); //undefined
console.log(req.body.number); //undefined
}
else{
Contact.create({
name:req.body.name,
email:req.body.email,
number:req.body.number,
address:req.body.address,
done: false
},function(err,contact){
if(err)
res.send(err);
Contact.find({},function(err,contact){
if(err)
res.send(err);
res.json(contact);
});
});
}
});
app.listen(8080);
console.log('App listening on port 8080');
home.ts
import { Component } from '@angular/core';
import { NavController, ModalController } from 'ionic-angular';
import { ContactsService } from '../../providers/contacts';
import { AddContactPage } from '../add-contact-page/add-contact-page';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
contacts: any;
constructor(public modalCtrl: ModalController, public contactsService: ContactsService, public nav: NavController ) {
}
ionViewDidLoad() {
console.log('inside ion view did load function');
this.contactsService.getContacts().then(data => {
console.log('inside Homepage get call', data);//object
this.contacts = data;
});
}
save() {
console.log('inside save function');
let modal = this.modalCtrl.create(AddContactPage);
modal.onDidDismiss(contact => {
if (contact) {
console.log('The new contacts that has to be added are', contact);//real data***
this.contacts.push(contact);
this.contactsService.createContacts(contact);
}
});
modal.present();
}
}
add-contact-page.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ViewController } from 'ionic-angular';
/**
* Generated class for the AddContactPage page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-add-contact-page',
templateUrl: 'add-contact-page.html',
})
export class AddContactPage {
name:any;
email:any;
number:number;
address:any;
constructor(public viewCtrl: ViewController) {
}
addContact(){
console.log('you have clicked addContact()');
let contact = {
name: this.name,
email:this.email,
number:this.number,
address:this.address
};
this.viewCtrl.dismiss(contact);
console.log('The data is', this.name, this.email, this.number, this.address);
}
close(){
this.viewCtrl.dismiss();
}
}
在home.ts文件中有一个模态,当我点击onadd按钮时,save()作为处理程序,add-contact-page被显示为模态窗口。我添加名称,电子邮件,号码,地址和单击addContact按钮处理程序。
.service class
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the Contacts provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ContactsService {
data: any;
constructor(public http: Http) {
console.log('Hello Contacts Provider');
this.data = null;
}
getContacts() {
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.http.get('http://localhost:8080/contacts').map(res => res.json()).subscribe(data => {
this.data = data;
console.log('inside get contactsService', this.data);//only objects
resolve(this.data);
});
});
}
createContacts(contact) {
console.log('inside create contact service class', contact);//this is correctly displaying the data
this.http.post('http://localhost:8080/contacts', JSON.stringify(contact)).subscribe(res => {
console.log('inside createContacts function in service class ', res.json());//only objects
console.log('contact in json',+JSON.stringify(contact) );//Nan
});
}
}
答案 0 :(得分:0)
在Srvice类post方法中,我没有传递contact
对象,所以它会是这样的:
this.http.post('url', contact, {headers:headers}).subscribe(res=>{console.log(res.json());
答案 1 :(得分:0)
由于它显示在控制台上,我建议您删除 {% for post in items %}
<p>{{ post }}</p>
{% endfor %}
JSON.stringify