utilizzo mysql in nodejs - crea createdAt e updateAt

misonsan

Nuovo Utente
31 Dic 2008
10
0
1
Utilizzo Mysql in Nodejs.
nel Model ho impostato le property di Users nel seguente modo:

module.exports = (sequelize, Sequelize) => {
const User = sequelize.define("users", {
id:{
// Sequelize module has INTEGER Data_Type.
type:Sequelize.INTEGER,
underscored: 0,

// To increment user_id automatically.
autoIncrement:true,

// user_id can not be null.
allowNull:false,

// For uniquely identify user.
primaryKey:true

},
cognome: {
type: Sequelize.STRING,
underscored: 0
},
nome: {
type: Sequelize.STRING,
underscored: 0
},
idStato: {
type: Sequelize.INTEGER,
underscored: 0
},
username: {
type: Sequelize.STRING,
underscored: 0
},
password: {
type: Sequelize.STRING,
underscored: 0
},
email: {
type: Sequelize.STRING,
underscored: 0
},
idRuolo: {
type: Sequelize.INTEGER,
underscored: 0
},
idRuolo_Day: {
type: Sequelize.INTEGER,
underscored: 0
},
noteutente: {
type: Sequelize.STRING,
underscored: 0
},
photo: {
type: Sequelize.STRING,
underscored: 0
},
remember_token: {
type: Sequelize.STRING,
underscored: 0
},
email_verified_at: {
type: Sequelize.DATE,
underscored: 0
},
key_utenti_operation: {
type: Sequelize.INTEGER,
underscored: 0
},
created_at: {
type: Sequelize.DATE,
underscored: 0
},
updated_at: {
type: Sequelize.DATE,
underscored: 0
},
},{
timestamps: true,
underscored: 0,
freezeTableName: true,
});

return User;
};

quando effettuo la login utilizzando il metodo login di authController

exports.login = (req, res) => {
User.findOne({
where: {
email: req.body.email
}
})
.then(user => {
if (!user) {
return res.status(404).send({ message: "email inesistente" });
} .......


con la User.findOne viene creata la stringa sql che deve effettuare la lettura su MySQL.

L'istruzione sql generata è la seguente:

Executing (default): SELECT `id`, `cognome`, `nome`, `idStato`, `username`, `password`, `email`, `idRuolo`, `idRuolo_Day`, `noteutente`, `photo`, `remember_token`, `email_verified_at`, `key_utenti_operation`, `created_at`, `updated_at`, `createdAt`, `updatedAt` FROM `users` AS `users` WHERE `users`.`username` = 'misonsan' LIMIT 1;

per errore vengono estratti anche i campi `createdAt`, `updatedAt` che NON ESISTONO

la select va in errore 1054 ER_BAD_FIELD_ERROR che qui sotto riporto.



ecco la segnalazione d'errore generata

name: 'SequelizeDatabaseError',
parent: Error: Unknown column 'createdAt' in 'field list'
at Packet.asError (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\commands\command.js:29:26)
at Connection.handlePacket (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:456:32)
at PacketParser.onPacket (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:85:12)
at PacketParser.executeStart (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:92:25)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10) {
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'createdAt' in 'field list'",
sql: "SELECT `id`, `cognome`, `nome`, `idStato`, `username`, `password`, `email`, `idRuolo`, `idRuolo_Day`, `noteutente`, `photo`, `remember_token`, `email_verified_at`, `key_utenti_operation`,
`created_at`, `updated_at`, `createdAt`, `updatedAt` FROM `users` AS `users` WHERE `users`.`username` = 'misonsan' LIMIT 1;",
parameters: undefined
},
original: Error: Unknown column 'createdAt' in 'field list'
at Packet.asError (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\commands\command.js:29:26)
at Connection.handlePacket (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:456:32)
at PacketParser.onPacket (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:85:12)
at PacketParser.executeStart (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Coding\Projects\CRUD_Sif\backend\node_modules\mysql2\lib\connection.js:92:25)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10) {
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'createdAt' in 'field list'",
sql: "SELECT `id`, `cognome`, `nome`, `idStato`, `username`, `password`, `email`, `idRuolo`, `idRuolo_Day`, `noteutente`, `photo`, `remember_token`, `email_verified_at`, `key_utenti_operation`,
`created_at`, `updated_at`, `createdAt`, `updatedAt` FROM `users` AS `users` WHERE `users`.`username` = 'misonsan' LIMIT 1;",
parameters: undefined
},
sql: "SELECT `id`, `cognome`, `nome`, `idStato`, `username`, `password`, `email`, `idRuolo`, `idRuolo_Day`, `noteutente`, `photo`, `remember_token`, `email_verified_at`, `key_utenti_operation`, `created_at`, `updated_at`, `createdAt`, `updatedAt` FROM `users` AS `users` WHERE `users`.`username` = 'misonsan' LIMIT 1;",
parameters: {}


Sicuramente è una personalizzazione parametrica mancante o non corretta.
Potete aiutarmi ?

Grazie

Moreno
 
Ho risolto il problema

Mettendo nel model i parametri


timestamps: false,
underscored: 0,



funziona a meraviglia.
 

Discussioni simili