79 Replies
How are you returning this response?
hmm I have ProductRepository, and Mapper configs
example code from repository:
`
Ooooof
Well, the code is bad, but it shouldn't result in any names starting with
$
Unless ProductDto
has some [JsonPropertyName]
attribute on the ID or somethingwell ProductDto looks like this:
also labels comes null in response as well, there is another problem lol
Huh, dunno where
$id
comes from then
How do you return the response itself?hmm i think like this:
If so, then no idea where the
$
s are coming fromoh i also have responsedto
That
object
is sus
Avoid it like the plagueand also label doesnt coming as well lol
oh lol
If you want a generic
Response
type, make it... generic
Still, I don't think that'd be an issueyeah 😄
maybe due to seeded data
`
Unlikely
Neither your model nor your DTO have an
Id
property, it's ProductId
i have ProductId too but response is messy
like this:
`
for example $values 🤔
Yes, that's what I'm talkinmg about. You have
ProductId
in your models, but the resonse has both ProductId
and $id
It kinda seems like it numbers the total objects in the JSON for some reason...?
Ah
I think I see what's going on
There are $ref
s that reference $id
soh i actually have this:
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
but when i deelte this then
Yeah
response gives error
saying cycle detected
Yeah
Because you're
.Include()
ing a bunch instead of doing the mapping on the database, I'd assume
Or because you're loading too much data that's referenced cyclicallyyeah i want to include everything
my data is small for now
Not a great idea, but sure, if it works for you right now so be it
You'll have to live with the
$id
s and $ref
s thoughoh i can just do:
`
why not a great idea
i want to show on my frontend
Could ignore the cycles, sure
Do you really need to show everything?
And nested so much, too?
Right now you have a list of products, each product has colours, each of those colours has products, each of those products has colours...
Why not just show the product, and the colour names?
hmm
Why do you need to load all of the products in green, when the user just wants to see the dress?
because in frontend people click the colour, and show that colour i think
there was product mockup in frontend
my products should look like this:
`
Yeah, the person clicks the colour
That loads a new view, with that colour and items associated with it
With a separate request
Don't try to just load your entire database to the client
hmmm
so this is bad model ? :
The user wants to see the dress. Cool, load the dress and related data.
The model is good
It's how you load the data that's causing the issues
Load the product, load its' colours, stop
Don't load all products associated with all the colours
oh
hmm
also this is get all products method
Well, yeah, you have it called
ProductId
not Id
this is for getproductbyid method right
Here, this will load all products
Still, it's just sample code
Adjust it accordingly
(and you can ditch the automapper in this case,
.Select()
does the job)bruh, dto -> model problem again
😩
though we didnt include mapper
maybe due to that it gave error ? 🤔
Your code says otherwise
As I said, adjust the code accordingly
If your
Label
has a list of names instead of a single name, then you'll need to change it
For exampleit just says cannot convert, doesnt give why
lol
Read the error
You have
IEnumerable<ModelDto>
where ICollection<Colour>
is expectedso i need to say new Colour instead ?
ohhh
now i got it
public ICollection<Colour> Colours { get; set; }
productdto has this
should be ColourDto probablyt
Yep
error changed now
still error 🤔
ICollection - IEnumerable incompatibility lol
Use a
List
instead of ICollection
maybehmm is list ienumarable ?
hmm it strictly wants that lol
weird
public IEnumerable<ColourDto> Colours { get; set; }
would that break code ?
Add a
.ToList()
after the .Select()
thenah
but it cries in colour part though lol
Cries how?
also what Ambiguous invocation: means btw
Means it doesn't know which overload of a given method it's supposed to call
oh
it gives that error for select
And it's ambiguous between shich methods?
hard to read
Uh, stick
.AsQueryable()
before the .Select()
s?hmm now trying to load LabelProducts, is that good idea ?
its a join table
main select gives erorr
not those
_context.Products.Select one
Ah, well, if you have an explicit many-to-many set up you might need some other mapping inside of the
.Select()
. Like Id = l.Label.Id
instead of Id = l.Id
Ah, huh
What is _context.Products
?
DbSet<Product>
I'd assume?public DbSet<Product> Products { get; set; }
yeah
Looks fine
Show me your current query code
to show errors, ss'ed instead
code itself:
`
Weird, that looks good
it has 4 errors lol
Try adding
.AsQueryable()
thereweirdly
similar error
maybe its automapper error ?
hmm also this error...
figured out, but still cant load label 🤔
`
@ZZZZZZZZZZZZZZZZZZZZZZZZZ seems original problem is l.Label part
if i delete that, all problems gone
then i cant fetch label
confusing part is querying many-to-many relationships in linq