0

I'm actually creating an app for my neighborhood little business, y have this db schema using Postgresql:

https://res.cloudinary.com/dthhugesg/image/upload/v1569939688/nodejserror/lugarlov_schema_db_dykxnt.png

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:

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Esteban
  • 485
  • 1
  • 4
  • 6
  • Can you show us the User model ? – Amadou Beye Oct 01 '19 at 15:39
  • offcourse ill do: https://res.cloudinary.com/dthhugesg/image/upload/v1569945561/nodejserror/lugarlove_usersmd_rtwfpv.png – Esteban Oct 01 '19 at 15:57
  • JSON doesn't accept circular object. Try to override the stringify method of JSON as said in this link: https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format – Amadou Beye Oct 01 '19 at 16:34
  • @AmadouBeye i think that is a query error, because it does not return result. – Esteban Oct 01 '19 at 16:53

0 Answers0