Hi everybody,
I would like to set up a search on the last name and first name of my users. In my old version I had deployed this code, which I also deployed on my new version:
forest/user.js
const { collection } = require('forest-express-sequelize');
const models = require('../models/');
const Op = models.objectMapping.Op;
collection('user', {
actions: [],
fields: [{
field: 'firstName',
type: 'String',
isFilterable: true,
get: (user) => {
return models.userData
.findOne({ where: { userId: user.id } })
.then((data) => {
return data ? data.firstName : '';
});
}
}, {
field: 'lastName',
type: 'String',
isFilterable: true,
get: (user) => {
return models.userData
.findOne({ where: { userId: user.id } })
.then((data) => {
return data ? data.lastName : '';
});
}
}, {
field: 'fullName',
type: 'String',
isFilterable: true,
get: (user) => {
return models.userData
.findOne({ where: { userId: user.id } })
.then((data) => {
return data ? data.firstName + ' ' + data.lastName : '';
});
},
search: function (query, search) {
let split = search.split(' ');
var searchCondition = {
[Op.or]: [
{ '$userData.firstName$': { [Op.like]: `%${split[0]}%` } },
{ '$userData.lastName$': { [Op.like]: `%${split[1]}%` } },
{ '$userData.firstName$': { [Op.like]: `%${split[1]}%` } },
{ '$userData.lastName$': { [Op.like]: `%${split[0]}%` } },
]
};
query.where[Op.and][0][Op.or].push(searchCondition);
return query;
}
}],
segments: [],
});
Only now I get this error :
[forest] 🌳🌳🌳 Unexpected error: Unknown column 'userData.firstName' in 'where clause'
{
"name": "SequelizeDatabaseError",
"parent": {
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'userData.firstName' in 'where clause'",
"sql": "SELECT count(`user`.`id`) AS `count` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `data` ON `user`.`id` = `data`.`userId` LEFT OUTER JOIN `UserIntegration` AS `integration` ON `user`.`id` = `integration`.`userId` WHERE ((lower(`user`.`email`) LIKE lower('%wan%') OR lower(`user`.`password`) LIKE lower('%wan%') OR lower(`user`.`indicMobile`) LIKE lower('%wan%') OR lower(`user`.`mobile`) LIKE lower('%wan%') OR lower(`user`.`emailValidated`) LIKE lower('%wan%') OR lower(`user`.`resetPassword`) LIKE lower('%wan%') OR (`user`.`mobile` LIKE '%wan%') OR (`userData`.`firstName` LIKE '%wan%' OR `userData`.`lastName` LIKE '%undefined%' OR `userData`.`firstName` LIKE '%undefined%' OR `userData`.`lastName` LIKE '%wan%')));"
},
"original": {
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'userData.firstName' in 'where clause'",
"sql": "SELECT count(`user`.`id`) AS `count` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `data` ON `user`.`id` = `data`.`userId` LEFT OUTER JOIN `UserIntegration` AS `integration` ON `user`.`id` = `integration`.`userId` WHERE ((lower(`user`.`email`) LIKE lower('%wan%') OR lower(`user`.`password`) LIKE lower('%wan%') OR lower(`user`.`indicMobile`) LIKE lower('%wan%') OR lower(`user`.`mobile`) LIKE lower('%wan%') OR lower(`user`.`emailValidated`) LIKE lower('%wan%') OR lower(`user`.`resetPassword`) LIKE lower('%wan%') OR (`user`.`mobile` LIKE '%wan%') OR (`userData`.`firstName` LIKE '%wan%' OR `userData`.`lastName` LIKE '%undefined%' OR `userData`.`firstName` LIKE '%undefined%' OR `userData`.`lastName` LIKE '%wan%')));"
},
"sql": "SELECT count(`user`.`id`) AS `count` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `data` ON `user`.`id` = `data`.`userId` LEFT OUTER JOIN `UserIntegration` AS `integration` ON `user`.`id` = `integration`.`userId` WHERE ((lower(`user`.`email`) LIKE lower('%wan%') OR lower(`user`.`password`) LIKE lower('%wan%') OR lower(`user`.`indicMobile`) LIKE lower('%wan%') OR lower(`user`.`mobile`) LIKE lower('%wan%') OR lower(`user`.`emailValidated`) LIKE lower('%wan%') OR lower(`user`.`resetPassword`) LIKE lower('%wan%') OR (`user`.`mobile` LIKE '%wan%') OR (`userData`.`firstName` LIKE '%wan%' OR `userData`.`lastName` LIKE '%undefined%' OR `userData`.`firstName` LIKE '%undefined%' OR `userData`.`lastName` LIKE '%wan%')));",
"stack": "SequelizeDatabaseError: Unknown column 'userData.firstName' in 'where clause'\n at Query.formatError (/Users/bvelluet/Git/Alfred/node_modules/sequelize/lib/dialects/mysql/query.js:243:16)\n at Query.handler [as onResult] (/Users/bvelluet/Git/Alfred/node_modules/sequelize/lib/dialects/mysql/query.js:50:23)\n at Query.execute (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/commands/command.js:30:14)\n at Connection.handlePacket (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:425:32)\n at PacketParser.onPacket (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:75:12)\n at PacketParser.executeStart (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/packet_parser.js:75:16)\n at Socket.<anonymous> (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:82:25)\n at Socket.emit (node:events:369:20)\n at addChunk (node:internal/streams/readable:313:12)\n at readableAddChunk (node:internal/streams/readable:288:9)\n at Socket.Readable.push (node:internal/streams/readable:227:10)\n at TCP.onStreamRead (node:internal/stream_base_commons:190:23)"
}
The userData table does exist, there is no problem in displaying smart field information.
I used this research method here:
Can you tell me if it is no longer current or if I did not implement it correctly ?
Thank’s,
See you,