al momento de utilizar insomia para crear customer me arroja que el userId is not allowed
{
"statusCode": 400,
"error": "Bad Request",
"message": ""userId" is not allowed"
}
costumer.models.js
const { Model, DataTypes, Sequelize } = require('sequelize');
const { USER_TABLE } = require('./user.model')
const CUSTOMER_TABLE = 'customers';
const CustomerSchema = {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
allowNull: false,
type: DataTypes.STRING,
},
lastName: {
allowNull: false,
type: DataTypes.STRING,
field: 'last_name',
},
phone: {
allowNull: true,
type: DataTypes.STRING,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
field: 'created_at',
defaultValue: Sequelize.NOW,
},
userId: {
field: 'user_id',
allowNull: false,
type: DataTypes.INTEGER,
unique: true,
references: {
model: USER_TABLE,
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
}
}
class Customer extends Model {
static associate(models) {
this.belongsTo(models.User, {as: 'user'});
}
static config(sequelize) {
return {
sequelize,
tableName: CUSTOMER_TABLE,
modelName: 'Customer',
timestamps: false
}
}
}
module.exports = { Customer, CustomerSchema, CUSTOMER_TABLE };
costumer.schema.js
const Joi = require('joi');
const id = Joi.number().integer();
const name = Joi.string().min(3).max(30);
const lastName = Joi.string();
const phone = Joi.string();
const userId = Joi.number().integer();
const email = Joi.string().email();
const password = Joi.string();
const getCustomerSchema = Joi.object({
id: id.required(),
});
const createCustomerSchema = Joi.object({
name: name.required(),
lastName: lastName.required(),
phone: phone.required(),
user: Joi.object({
email: email.required(),
password: password.required()
})
});
const updateCustomerSchema = Joi.object({
name,
lastName,
phone,
userId
});
module.exports = { getCustomerSchema, createCustomerSchema, updateCustomerSchema };
customers.service.js
const boom = require('@hapi/boom');
const { models } = require('../libs/sequelize');
class CustomerService {
constructor() {}
async find() {
const rta = await models.Customer.findAll({
include: ['user']
});
return rta;
}
async findOne(id) {
const user = await models.Customer.findByPk(id);
if (!user) {
throw boom.notFound('customer not found');
}
return user;
}
async create(data) {
const newCustomer = await models.Customer.create(data, {
include: ['user']
});
return newCustomer;
}
async update(id, changes) {
const model = await this.findOne(id);
const rta = await model.update(changes);
return rta;
}
async delete(id) {
const model = await this.findOne(id);
await model.destroy();
return { rta: true };
}
}
module.exports = CustomerService;