Home > Mobile >  Querying String data from database into array Sequelize
Querying String data from database into array Sequelize

Time:07-11

So i basically trying to get string value from database into an Array using Sequelize:

{
    "status": "success",
    "data": [
        {
            "id": 15,
            "title": "The Godfather",
            "year": 1972,
            "director": "Francis Ford Coppola",
            "categoryFilm": "R",
                "Url": "https://m.media-amazon.com/images/M/MV5BMTU4MTgxOTQ0Nl5BMl5BanBnXkFtZTgwNDI0Mjk1NDM@._V1_UY100_CR19,0,100,100_AL_.jpg,https://m.media-amazon.com/images/M/MV5BMTczMTk5MjkwOF5BMl5BanBnXkFtZTgwMDI0Mjk1NDM@._V1_UY100_CR12,0,100,100_AL_.jpg,https://m.media-amazon.com/images/M/MV5BZTFiODA5NWEtM2FhNC00MWEzLTlkYjgtMWMwNzBhYzlkY2U3XkEyXkFqcGdeQXVyMDM2NDM2MQ@@._V1_UX100_CR0,0,100,100_AL_.jpg",
                "genre": [
                    {
                        "genre": "Crime,Drama"
                    }
                ]
            }
        ]
    }

with my code showed as below, i try sequelize literal to change it but i cannot resolve how to turn it from string into array and split it by "," (comma):

    const allFilm = async (req, res) => {
        await Film.findAll({
            attributes: [
                "id",
                "title",
                "year",
                "director",
                [sequelize.literal(`"category"."category"`), "categoryFilm"],
                [sequelize.literal(`"photo"."photoUrl"`), "Url"],
            ],
            subQuery: false,
            include: [
                {
                    model: Genre,
                    as: "genre",
                    attributes: ["genre"],
                },
                {
                    model: Category,
                    as: "category",
                    attributes: [],
                },
                {
                    model: Photo,
                    as: "photo",
                    attributes: [],
                },
            ],
        })
            .then((data) => {
                res.status(200).json({
                    status: "success",
                    data: data,
                });
            })
            .catch((err) => {
                res.status(400).json({
                    status: err,
                });
            });
    };

what i want is like this is there is somthing wrong with my code since i find no error but i can not resolved it into array:

"Url": ["https://m.media-amazon.com/images/M/MV5BMTU4MTgxOTQ0Nl5BMl5BanBnXkFtZTgwNDI0Mjk1NDM@._V1_UY100_CR19,0,100,100_AL_.jpg","https://m.media-amazon.com/images/M/MV5BMTczMTk5MjkwOF5BMl5BanBnXkFtZTgwMDI0Mjk1NDM@._V1_UY100_CR12,0,100,100_AL_.jpg","https://m.media-amazon.com/images/M/MV5BZTFiODA5NWEtM2FhNC00MWEzLTlkYjgtMWMwNzBhYzlkY2U3XkEyXkFqcGdeQXVyMDM2NDM2MQ@@._V1_UX100_CR0,0,100,100_AL_.jpg"],

CodePudding user response:

I can't answer your question, but you can maybe try that : From Load attributes from associated model in sequelize.js

await Film.findAll({
  attributes: [
    "id",
    "title",
    "year",
    "director",
    [sequelize.literal(`"category"."category"`), "categoryFilm"],
    [Sequelize.col('photo.photoUrl'), 'Url'] // here
  ],
  subQuery: false,
  raw:true, // here
  include: [
    {
      model: Genre,
      as: "genre",
      attributes: ["genre"],
    },
    {
      model: Category,
      as: "category",
      attributes: [],
    },
    {
      model: Photo,
      as: "photo",
      attributes: [],
      required: false, // here
     },
   ],
})
  • Related