I'm currently setting up a sailsjs app, I've set up my psql database, models and controllers, only I'm getting a problem when I use the .populate() function. It's a gallery website for an artist and what I want to do is get the galleries with one painting for the hompage where the galleries will be displayed with one of the paintings in them.
This is my code
models/Gallery.js
module.exports = {
tableName: 'gallery',
primaryKey: 'id',
attributes: {
id:{
type: 'number',
unique: true,
autoIncrement: true
},
name:{
type: 'string',
required: true
},
paintings:{
collection: 'painting',
via: 'gallery'
},
createdAt:{
type: 'ref',
columnName: 'created_at',
autoCreatedAt: true
},
updatedAt:{
type: 'ref',
columnName: 'updated_at',
autoUpdatedAt: true
}
},
};
models/Painting.js
module.exports = {
tableName: 'painting',
primaryKey: 'id',
attributes: {
id:{
type: 'number',
unique: true,
autoIncrement: true
},
name: {
type: 'string',
required: true,
},
height:{
type: 'number'
},
width:{
type: 'number'
},
description:{
type: 'string'
},
minPrice:{
type: 'number',
columnName: 'min_price'
},
img:{
type: 'string',
columnName: 'img_url',
},
isSold:{
type: 'boolean',
defaultsTo: false,
columnName: 'is_sold'
},
gallery:{
model: 'gallery',
columnName: 'gallery_id'
},
tags:{
collection: 'tag',
via: 'paintings',
through: 'paintinghastag'
},
commentaries:{
collection: 'commentary',
via: 'painting',
},
offers:{
collection: 'offer',
via: 'painting'
},
createdAt:{
type: 'ref',
columnName: 'created_at',
autoCreatedAt: true
},
updatedAt:{
type: 'ref',
columnName: 'updated_at',
autoUpdatedAt: true
}
},
};
The function in the controller
getGalleriesWithFirstPainting: async (req, res) =>{
try {
const galleries = await Gallery.find().populate('paintings', {limit: 1});
console.log(galleries);
return res.ok({
success: true,
galleries
});
} catch (error) {
console.log(error);
return res.serverError({
success: false,
message: error.message
});
}
},
I have setup a route and when I call it, if I remove the .populate(), it works, but when I keep it I get this error:
at Object.getGalleriesWithFirstPainting [as gallery/getgallerieswithfirstpainting] ( /app/api/controllers/GalleryController.js:13:39)
... 14 lines matching cause stack trace ...
at Layer.handle [as handle_request] ( /app/node_modules/express/lib/router/layer.js:95:5) {
cause: Error [AdapterError]: Unexpected error from database adapter: Cannot read properties of undefined (reading 'definition')
at Object.getGalleriesWithFirstPainting [as gallery/getgallerieswithfirstpainting] ( /app/api/controllers/GalleryController.js:13:39)
at /app/node_modules/sails/lib/router/bind.js:236:53
at routeTargetFnWrapper ( /app/node_modules/sails/lib/router/bind.js:395:9)
at Layer.handle [as handle_request] ( /app/node_modules/express/lib/router/layer.js:95:5)
at next ( /app/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch ( /app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] ( /app/node_modules/express/lib/router/layer.js:95:5)
at /app/node_modules/express/lib/router/index.js:281:22
at Function.process_params ( /app/node_modules/express/lib/router/index.js:335:12)
at next ( /app/node_modules/express/lib/router/index.js:275:10)
at next ( /app/node_modules/express/lib/router/route.js:127:14)
at Hook.expressMiddleware ( /app/node_modules/sails/lib/hooks/i18n/index.js:205:14)
at Hook.wrapper [as expressMiddleware] ( /app/node_modules/@sailshq/lodash/lib/index.js:3282:19)
at addLocalizationMethod ( /app/node_modules/sails/lib/hooks/i18n/index.js:147:35)
at routeTargetFnWrapper ( /app/node_modules/sails/lib/router/bind.js:395:9)
at Layer.handle [as handle_request] ( /app/node_modules/express/lib/router/layer.js:95:5) {
adapterMethodName: 'join',
modelIdentity: 'gallery',
raw: TypeError: Cannot read properties of undefined (reading 'definition')
at iterator ( /app/node_modules/sails-postgresql/helpers/private/query/process-each-record.js:53:20)
at iterateChildRecords ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:178:11)
at arrayEach ( /app/node_modules/@sailshq/lodash/lib/index.js:1470:13)
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3532:13)
at iterateAttributes ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:174:11)
at /app/node_modules/@sailshq/lodash/lib/index.js:3260:15
at baseForOwn ( /app/node_modules/@sailshq/lodash/lib/index.js:2230:14)
at /app/node_modules/@sailshq/lodash/lib/index.js:3230:18
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3533:13)
at iterateRecords ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:125:7)
at arrayEach ( /app/node_modules/@sailshq/lodash/lib/index.js:1470:13)
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3532:13)
at eachRecordDeep ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:113:5)
at Object.processEachRecord ( /app/node_modules/sails-postgresql/helpers/private/query/process-each-record.js:49:3)
at releaseConnectionCb ( /app/node_modules/sails-postgresql/helpers/join.js:402:29)
at Object.success ( /app/node_modules/sails-postgresql/helpers/private/connection/release-connection.js:37:14)
},
isOperational: true,
adapterMethodName: 'join',
modelIdentity: 'gallery',
raw: TypeError: Cannot read properties of undefined (reading 'definition')
at iterator ( /app/node_modules/sails-postgresql/helpers/private/query/process-each-record.js:53:20)
at iterateChildRecords ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:178:11)
at arrayEach ( /app/node_modules/@sailshq/lodash/lib/index.js:1470:13)
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3532:13)
at iterateAttributes ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:174:11)
at /app/node_modules/@sailshq/lodash/lib/index.js:3260:15
at baseForOwn ( /app/node_modules/@sailshq/lodash/lib/index.js:2230:14)
at /app/node_modules/@sailshq/lodash/lib/index.js:3230:18
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3533:13)
at iterateRecords ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:125:7)
at arrayEach ( /app/node_modules/@sailshq/lodash/lib/index.js:1470:13)
at Function.<anonymous> ( /app/node_modules/@sailshq/lodash/lib/index.js:3532:13)
at eachRecordDeep ( /app/node_modules/waterline-utils/lib/each-record-deep/index.js:113:5)
at Object.processEachRecord ( /app/node_modules/sails-postgresql/helpers/private/query/process-each-record.js:49:3)
at releaseConnectionCb ( /app/node_modules/sails-postgresql/helpers/join.js:402:29)
at Object.success ( /app/node_modules/sails-postgresql/helpers/private/connection/release-connection.js:37:14)
}
error: Sending 500 ("Server Error") response:
{
success: false,
message: "Unexpected error from database adapter: Cannot read properties of undefined (reading 'definition')"
}
I've tried to drop and recreate the database, I dont think it comes from my models, I'm really lost and I can't fin any post on any forum or issue about someone getting this error. What I really don't get is this "reading 'definiton". I don't understand how it can be linked to this .populate() thing. Also this problem exists only for the Gallery model/controller, it works on the other models.
I found the answer !! It turns out I had another model called Shipping in which I put a 'tableName: 'painting'' instead of 'tableName: 'shipping''. So th error occured becaused it had two different models reffering to the same table. I didn't see it because I kept checking the models direclty concerned by my call (gallery and painting).