const mongoose = require('mongoose');
const addressSchema = mongoose.Schema({
type: String,
street: String,
city: String,
state: String,
country: String,
postalcode: Number
});
const createStudentSchema = mongoose.Schema({
admisionnum: Number,
first: String,
last: String,
phone: String,
address : [addressSchema],
isActive : { type : Boolean, default: true}
}, {
timestamps: true
});
module.exports = mongoose.model('createStudent', createStudentSchema);
Addreschema
для адреса в массиве объектов.Я получаю ноль на выходе для json
ниже.
Это json
ввод
{
"admisionnum":"1",
"first": "Dan",
"last": "HEllo",
"phone": "9000909575",
"address" : [
{
"type": "own",
"street": "18 ksdhks",
"city": "chennai",
"state": "Tamil Nadhu",
"country": "India",
"postalcode": "600097"
}
],
"isActive": "true"
}
Response Json
вывод
{
"isActive": true,
"_id": "5c9e57f718e3de2ca4dd1d86",
"admisionnum": 1,
"first": "Jesus",
"last": "christ",
"phone": "9000909575",
"address": [
null,
null,
null,
null,
null,
null
],
"createdAt": "2019-03-29T17:37:59.291Z",
"updatedAt": "2019-03-29T17:37:59.291Z",
"__v": 0
}
Пожалуйста, помогитеКак связать данные для адреса.код контроллера ниже
exports.createStudent = (req, res) => {
// Create a Note
const CreateStudent = new createStudent({
admisionnum: req.body.admisionnum,
first: req.body.first,
last: req.body.last,
phone: req.body.phone,
address : [
req.body.address.type,
req.body.address.street,
req.body.address.city,
req.body.address.state,
req.body.address.country,
req.body.address.postalcode
] ,
isActive: req.body.type
});
// Save Note in the database
CreateStudent.save()
.then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while creating the Note."
});
});
};