Hey guys, I'm trying to check existence of some re...
# orm-help
k
Hey guys, I'm trying to check existence of some records base on multiple fields since there's no feature like unique together in prisma yet i came with the idea of checking existence of them before creating, But in my case i have an array of objects and i have to map through them... finally i wrote it out like this:
Copy code
musics.map(async (music) => {
            let isMusicExists = await context.prisma.$exists.musicDirectory({
                AND: [
                    {
                        directoryId,
                        musics_some: {
                            trackId: music.trackId,
                            trackService: music.trackService
                        }
                    }
                ]
            });
            if (isMusicExists){
                throw new Error('This music is already added in this place!')
            }
        });
this piece of code is in middle of my revolver, What i expect after receiving duplicate record is throwing an error and stop the execution furthermore but what i get in console is:
Copy code
(node:11309) UnhandledPromiseRejectionWarning: Error: This music is already added in this place!
    at musics.map (/home/kianoosh/MyApp/resolvers/Mutation.js:384:23)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:11309) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
I tried to use catch but it didn't make any difference after all the error is just printing in console and doesn't stop anything.
c
you’re getting an unhandled promise rejection since the error is being thrown inside the async function of your map. you’d have to await your array of promises so that the error throws in your resolver
Copy code
await Promise.all(musics.map(async (music) => {
👍 1
j
^ just what i was typing up
you can also use
for ... in
for in
will make them fire one after the other. Promise.all will fire them all at the same time.
👍 1
k
@carlo Tnx, it helped me a lot.
@Jared So u mean
for in
is more optimized than
Promise.all
?