Reputation: 2959
Maybe is a stupid question but I would like to know how to post in a JSON object. I am new in this backend stuff so I am still learning.
Here is my JSON scheme.
email: {
type: String,
unique: true,
lowercase: true,
required: 'Email address is required',
validate: [validateEmail, 'Please enter a valid email']
},
password: {
type: String
},
role: {
type: String
},
firstName: {
type: String,
},
lastName: {
type: String
},
phone: {
type: Number,
unique: true
},
address: [
{
number: {type: String},
street: {type: String},
city: {tyle: String},
county: {type: String},
postcode: {type: String}
}
],
And here I am making the post.
User.findOne({email: email}, function(err, existingUser) {
if (err) { return next(err) }
if (existingUser) {return res.status(422).json({error: "Email taken"})}
var user = new User({
email: email,
password: password,
role: role,
firstName: firstName,
lastName: lastName,
phone: phone,
number: number,
street: street,
city: city,
county: county,
postcode: postcode
});
So everything is posting apart of address strings. In DB I can see only "address" : []
Here is my signUp function
exports.signup = function(req, res, next) {
var email = req.body.email;
var password = req.body.password;
var role = req.body.role;
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var phone = req.body.phone;
var number = req.body.number;
var street = req.body.street;
var city = req.body.city;
var county = req.body.county;
var postcode = req.body.postcode;
if (!email || !password) {
return res.status(422).json({error: "You must provide an email and password"});
}
Upvotes: 0
Views: 44
Reputation: 5469
You should send the street
, city
, country
and postcode
in address key as you are expecting these under address
like this:
User.findOne({email: email}, function(err, existingUser) {
if (err) { return next(err) }
if (existingUser) {return res.status(422).json({error: "Email taken"})}
var user = new User({
email: email,
password: password,
role: role,
firstName: firstName,
lastName: lastName,
phone: phone,
number: number,
address: [{
street: street,
city: city,
county: county,
postcode: postcode
}]
});
Upvotes: 2