NodeJS API Mongodb不保存数据

时间:2017-04-03 16:39:59

标签: node.js mongodb mongoose nodeapi

最近我开始创建一个nodejs和mongo API,尝试保存数据,但不能保存任何信息。唯一可以得到的信息是发出POST请求时生成的ID。我使用POSTMANT测试de API,但没有成功保存数据。唯一可以获得的信息就是ID,这是我的代码。

这是API路由和模型位于同一文件api objects中。 api.js

const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();

// MongoDb URL 

const dbHost = 'mongodb://localhost/motiva';

//conectamos la BD

mongoose.connect(dbHost, function(err) {
    if(err){
    console.log('No nos podemos conectar a la BD')
   }else {
    console.log('Conexion Exitosa')
  }
});

// Creamos el Schema  de la BD

const applicantSchema = new mongoose.Schema({
workbefore: {type:String, enum: ['Yes', 'No']},
payrange: String,
desposition: {type:String, enum: ['Costumer Service Representative', 
'Spanish Costumer Service', 'Claims Representative', 'Salex 
Executive', 'Other']},
name: String,
psourname: String,
msourname: String,
dob: Date,
age: Number,
gender: {type:String, enum:['Male', 'Female']},
pofbirth: String,
nationality: String,
city: {type:String, enum: ['Tijuana', 'Tecate', 'Mexicali', 
'Rosarito', 'Ensenada']},
satate: {type:String, enum: ['Baja California', 'Other']},
zipcode: Number,
street: String,
streetNumber: String,
appartNumber: String,
homePhone: Number,
mobileNumber: Number,
secondaryPhone: String,
radio: String,
email: String,
relation: {type: String, enum: ['Single', 'Married', 'Separated', 
'Living Together']},
spouseName: String,
childrens:{ type: String, enum: ['1', '2', '3', '4', '5', 'Other']},
fatherName: String,
motherName: String,
emergencyContact: String,
relationshipContact: String,
relcontactPhone: String,
dependentYou: String,
timeResident: String,
education: String,
school: String,
graduationDate: Date,
degree: String,
englishProficiency: Number,
englishWriteLevel: String,
computerProficiency:{type: String, enum:['Basic', 'Intermediate', 
'Advanced']},
validVisa:{type: String, enum: ['Yes', 'No']},
bodyTattos:{type: String, enum: ['Yes', 'No']},
memberClub: String,
criminalRecord:{type: String, enum: ['Yes', 'No']},
prisionMexico:{type: String, enum: ['Yes', 'No']},
shift:{type: String, enum: ['Morning', 'Afternoon', 'Night']},
callWork:{type: String, enum: ['Yes', 'No']},
nightShift:{type: String, enum: ['Yes', 'No']},
refName: String,
refOccupation: String,
refPhone: String,
refKnow: String,
refEmail: String,
workExperince:{type: String, enum: ['Yes', 'No']},
companyName: String,
companyCountry: String,
companyDate: Date,
leaveJob: String,
jobTitle: String,
supervisorName: String,

});

// Creamos el Modelo 

const Applicant = mongoose.model('Applicant', applicantSchema);

// Obtenemos todos los Aplicantes GET ALL
router.get('/applicants', (req, res) => {
Applicant.find({}, (err, applicants) => {
    if (err) res.status(500).send(error)
    res.status(200).json(applicants);
  });
});

// Obetenmos los Aplicantes de Manera Indivudual

router.get('/applicants/:id', (req, res) => {
    Applicant.findById.apply(req.param.id, (err, applicants) => {
    if(err) res.status(500).send(error)
    res.status(200).json(applicants);
   });
});

//Creamos el Applicante en la Base de Datos
router.post('/applicants', (req, res) => {
var applicants = new Applicant();
applicants.workbefore = req.body.workbefore;
    applicants.payrange = req.body.payrange;
    applicants.desposition = req.body.desposition;
    applicants.name = req.body.name;
    applicants.psourname = req.body.psourname;
    applicants.msourname = req.body.msourname;
    applicants.dob = req.body.dob;
    applicants.age = req.body.age;
    applicants.gender = req.body.gender;
    applicants.pofbirth = req.body.pofbirth;
    applicants.nationality = req.body.nationality;
    applicants.city = req.body.city ;
    applicants.satate = req.body.satate;
    applicants.zipcode = req.body.zipcode;
    applicants.street = req.body.street;
    applicants.streetNumber = req.body.streetNumber;
    applicants.appartNumber = req.body.appartNumber;
    applicants.homePhone = req.body.homePhone;
    applicants.mobileNumber = req.body.mobileNumber;
    applicants.secondaryPhone = req.body.secondaryPhone;
    applicants.radio = req.body.radio;
    applicants.email = req.body.email;
    applicants.relation = req.body.relation;
    applicants.spouseName = req.body.spouseName;
    applicants.childrens = req.body.childrens;
    applicantsfatherName = req.body.fatherName;
    applicants.motherName = req.body.motherName;
    applicants.emergencyContact = req.body.emergencyContact;
    applicants.relationshipContact = req.body.relationshipContact;
    applicants.relcontactPhone = req.body.relcontactPhone;
    applicants.dependentYou = req.body.dependentYou;
    applicants.timeResident = req.body.timeResident;
    applicants.education = req.body.education;
    applicants.school = req.body.school;
    applicants.graduationDate = req.body.graduationDate;
    applicants.degree = req.body.degree;
    applicants.englishProficiency = req.body.englishProficiency;
    applicants.englishWriteLevel = req.body.englishWriteLevel;
    applicants.computerProficiency = req.body.computerProficiency;
    applicants.validVisa = req.body.validVisa;
    applicants.bodyTattos = req.body.bodyTattos;
    applicants.memberClub = req.body.memberClub;
    applicants.criminalRecord = req.body.criminalRecord;
    applicants.prisionMexico = req.body.prisionMexico;
    applicants.shift = req.body.shift;
    applicants.callWork = req.body.callWork;
    applicants.nightShift = req.body.nightShift;
    applicants.refName = req.body.refName;
    applicants.refOccupation = req.body.refOccupation;
    applicants.refPhone = req.body.refPhone;
    applicants.refKnow = req.body.refKnow;
    applicants.refEmail = req.body.refemail;
    applicants.workExperince = req.body.workExperince;
    applicants.companyName = req.body.companyName;
    applicants.companyCountry = req.body.companyCountry;
    applicants.companyDate = req.body.companyDate;
    applicants.leaveJob = req.body.leaveJob;
    applicants.jobTitle = req.body.jobTitle;
    applicants.supervisorName = req.body.supervisorName;  

    applicants.save(error => {
    if (error) res.status(500).send(error);

    res.status(201).json({
        message: 'Applicant created successfully'
     });
  });

});

// Obtenemos nuestro API List
router.get('/', (req,res) => {
res.send('API Works - API Funcionando');
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

您需要设置middleware以从req.body获取数据。我没有在您的代码中看到任何中间件设置。

1)包括以下代码以设置中间件

var bodyParser = require('body-parser');
var app = express();
var urlencoded_body_parser = bodyParser.urlencoded({
    extended: true
});

app.use(bodyParser.json());
app.use(urlencoded_body_parser);

2)在帖子/applicants端点

上添加控制台日志
console.log(req.body);

3)如果你在控制台上获得了数据,那么数据应该根据你已经完成的映射成功地保存在MongoDB上