2017-04-03 3 views
-1

최근에 nodejs 및 mongo API를 생성하고 데이터 저장을 시도하지만 정보를 저장할 수 없습니다. 유일한 정보는 POST 요청을 할 때 생성 된 ID입니다. API를 테스트하는 데 POSTMANT를 사용하지만 데이터를 저장하지는 못합니다. 유일한 정보는 얻을 수있는 ID는 여기에 내 코드입니다.NodeJS API Mongodb 데이터 저장 안 함

이것은 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; 
+0

https://stackoverflow.com/help/mcve를 읽으십시오. –

답변

0

당신은 req.body에서 데이터를 얻기 위해 설정 middleware해야합니다. 귀하의 코드에 미들웨어 설정이 표시되지 않습니다.

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에서 성공적으로 유지됩니다.

+0

감사합니다. 완벽한 –

+0

을 사용하십시오. 문제가 해결되면 답변을 수락 해주십시오. – notionquest