I'm actually creating an app for my neighborhood little business, y have this db schema using Postgresql:
my use cases are:
- 1 user can create 1 experience.
- 1 user can create a business local only if its going to leave an experience from it.
- 1 experience can have several images.
( I haven't a relationship between business_locals and users because doesn't matter for who creates the business).
I haven't had problems untill now, I'm trying to get the business with it's name, lat and lng; the average score given by users, 1 image from the last experience where the name like = "somestring"
this are my models:
Business Model: https://res.cloudinary.com/dthhugesg/image/upload/v1569939688/nodejserror/lugarlov_businessmd_rwnsnu.png
Images model: https://res.cloudinary.com/dthhugesg/image/upload/v1569939688/nodejserror/lugarlov_imgaesmd_m4hkzv.png
experiencies model: https://res.cloudinary.com/dthhugesg/image/upload/v1569939688/nodejserror/lugarlov_experienciesmd_f7jtxt.png
y try this code:
export async function getBusinessByQueryString(req, res) {
let query = req.params.query;
const businessesResult = await Business.findAll({
where: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
{
[Op.like]: `%${query}%`
}
),
attributes: ['name', sequelize.fn('COUNT', sequelize.col('experiencies.id'))],
include: [
{ model: Experiencies, attributes: [] }
],
group: ["business_locals.id"]
}).catch(err => {
return res.status(500).json({
ok: false,
messagge: "Error al consultar el comercio",
err
});
});
console.log(businessesResult);
// let businessIdArrays = businessesResult.map((obj) => obj.id);
// console.log(businessIdArrays);
return res.json({
ok: true,
message: businessesResult
});
}
but I'm having this error:
https://res.cloudinary.com/dthhugesg/image/upload/v1569939691/nodejserror/error_x0tsiq.png
I'm expecting this:
[
{
"id": 2,
"name": "Business local example 1",
"lat": 6.24523,
"lng": -75.4543,
"average_score": 4,
"experiences": [
{
"id": 1,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [
{
"url": "https://placeimg.com/640/490/tech"
},
{
"url": "https://placeimg.com/640/490/animals"
}
],
"user": {
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
},
{
"id": 1,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [
{
"url": "https://placeimg.com/640/490/tech"
},
{
"url": "https://placeimg.com/640/490/animals"
}
],
"user": {
"id": 2,
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
},
{
"id": 2,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [],
"user": {
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
},
{
"id": 3,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [],
"user": {
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
},
{
"id": 4,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [],
"user": {
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
}
]
},
{
"id": 3,
"name": "Business local example 2",
"lat": 6.24523,
"lng": -75.4543,
"average_score": 4,
"experiences": [
{
"id": 1,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [
{
"url": "https://placeimg.com/640/490/animals"
}
],
"user": {
"id": 2,
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
},
{
"id": 4,
"comment": "was a good experience to be there, good food and cool service",
"score_given": 4,
"created_at": "2019-09-30 18:55:00",
"images": [],
"user": {
"name": "Esteban nuevo nombre",
"phone": "0000000+"
}
}
]
}
]
thank you very much, all of you i hope that someone can help me and tell me if i have any mistake