-1
I am having the following error when trying to create a user in my api
Validationerror: address.0.status: Path
estado
is required. , Address.0.city: Pathcidade
is required. , address.0.neighborhood: Pathbairro
is required. , address.0.street: Pathlogradouro
is required. , address.1.cep: Pathcep
is required. , address.1.city: Pathcidade
is required. , address.1.neighborhood: Pathbairro
is required. , address1.street: Pathlogradouro
is required. , address2.status: Pathestado
is required. , address.2.cep: Pathcep
is required. , address.2.neighborhood: Pathbairro
is required. , address.2.street address: Pathlogradouro
is required. , address3.status: Pathestado
is required. , address3.cep: Pathcep
is required. , address.3.city: Pathcidade
is required. , Address.3.district: Pathbairro
is required. , address.4.status: Pathestado
is required. , address.4.cep: Pathcep
is required. , city address.4.: Pathcidade
is required. , address.4.street address: Pathlogradouro
is required.
I have the following code in the user model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');
const UserSchema = new Schema({
email: { type: String, required: true, unique: true, lowercase: true },
senha: {type: String, required: true, select: false},
nome: {type: String, required: true, select: true},
sobrenome: {type: String, required: true, select: true},
cpf: {type: String, required: true, select: true, unique: true,},
endereco: [{
logradouro: {type: String, required: true, select: true},
bairro: {type: String, required: true, select: true},
cidade: {type: String, required: true, select: true},
cep: {type: String, required: true, select: true},
estado: {type: String, required: true, select: true},
}],
created: {type: Date, default: Date.now}
});
UserSchema.pre('save', function (next) {
let user = this;
if(!user.isModified('senha')) return next();
bcrypt.hash(user.senha, 10, (err,encrypted) => {
user.senha = encrypted;
return next();
});
});
module.exports = mongoose.model('User', UserSchema);
and the following user controller code:
const bcrypt = require('bcryptjs');
const mongoose = require('mongoose');
const Users = require('../models/user');
const jwt = require('jsonwebtoken');
const createUserToken = (userId) => {
return jwt.sign({id: userId}, 'chavesecreta', {expiresIn: '7d'});
}
module.exports = {
buscar(req, res) {
Users.find({}, (err, data) => {
if (err) return res.send({ error: 'Erro na consulta de usuarios!' });
return res.send(data);
});
},
criar(req, res) {
const { email, senha, nome, sobrenome, cpf } = req.body;
if (!email || !senha || !nome || !sobrenome || !cpf ) return res.send({ error: "Dados não preenchidos!" });
Users.findOne({ email: email }, (err, data) => {
if (err) return res.send({ error: "Erro ao buscar usuario!" });
if (data) return res.send({ error: "Usuario já registrado!" });
Users.create(req.body, (err, data) => {
if (err) return res.send({ error: "Erro ao criar usuario!" + err });
data.senha = undefined;
return res.send({data, token: createUserToken(data.id)});
});
});
},
auth(req, res) {
const { email, senha } = req.body;
if (!email || !senha) return res.send({ error: "Preencha todos os campos!" });
Users.findOne({ email: email }, (err, data) => {
if (err) return res.send({ error: "Erro ao buscar usuario!" });
if (!data) return res.send({ error: "Usuario não registrado!" });
bcrypt.compare(senha, data.senha, (err, same) => {
if (!same) return res.send({ error: "Senha incorreta!" });
data.senha = undefined;
return res.send({data, token: createUserToken(data.id)});
})
}).select('+senha');
},
validacao(req, res) {
return res.send({status: "Valido"});
}
}
How can I fix this error? Thanks in advance
Staff who decline questions could at least state the reason
– Lucas Simao