This is so strange. I’m running a query and then m...
# orm-help
a
This is so strange. I’m running a query and then mapping the result with findUnique to get the user afterwards. For some reason though, it’s returning null users.
Copy code
const scorers = await DBConn.recognition_logger.groupBy({
            by: ['receiver_id'],
            _sum: {
                amount: true,
            },
            orderBy: {
                _sum: { amount: 'desc' },
            },
            where: {
                OR: [{ action: 'gave_points' }, { action: 'approve_claimable_award' }, { action: 'custom_award_send' }],
                users_recognition_logger_receiver_idTousers: {
                    active: true,
                },
                amount: { gt: 0 },
                company_id: res.locals.requestingUser.companies.id,
                created: {
                    gte: DateTime.now().startOf('month').startOf('day').toUnixInteger(),
                    lte: DateTime.now().endOf('month').endOf('day').toUnixInteger(),
                },
                hidden_from_leaderboard: false,
                valid: true,
                type: {
                    equals: 'Regular',
                },
            },
        });
        scorers.map(async (e) => {
            const user = await DBConn.users.findUnique({ where: { id: e.receiver_id } });
            console.log(user);
        });
receiver_id is a user’s id.
Copy code
SELECT [*] FROM "public"."users" WHERE "public"."users"."id" IN ($1,$2) OFFSET $3
Params: [157359532448138,165341040401209,0]
Duration: 11ms
null
null
Interestingly, when I copy this into pgAdmin, it works just fine. Any idea why this is happening? Apologies for the long message, I removed all the fields for security and length’s sake. (Yes I know that I’m mapping and it returns a new object. But the output from the Query in my console is null,null)
👀 1
n
Hello @AJ Holloway 👋 Just to confirm, can you log the value of
e
in the map callback function and check if the user indeed exists in the database with the id of
e.receiver_id
?
a
Yes the value of
e.receiver_id
is a number, the same type that a
user.id
is.
n
Can you change the map to a normal for loop and try? await in map would not work as expected, you would either need to return a promise from map callback function or use a normal for loop. Stack Overflow answer reference