DylanDev
DylanDev
CCConvex Community
Created by DylanDev on 12/20/2024 in #support-community
Deleting Users from Backend Function that are Synced with Clerk
No description
11 replies
CCConvex Community
Created by DylanDev on 11/7/2024 in #support-community
npx convex deploy does not send my dev data including files to production
Why does running npx convex deploy not send over my tables from my dev instance and files to the production instance?
4 replies
CCConvex Community
Created by DylanDev on 10/14/2024 in #support-community
Clerk With Convex in Backend Functions
I want to store a score variable in the public metadata but I can't figure out how to modify that from the backend and I get an error? Error:
Uncaught Error
at UserAPI.request (../node_modules/@clerk/backend/src/api/request.ts:186:15)
at async handler (../convex/game.ts:102:61)
Uncaught Error
at UserAPI.request (../node_modules/@clerk/backend/src/api/request.ts:186:15)
at async handler (../convex/game.ts:102:61)
Code:
import { v } from "convex/values";

import { mutation, query } from "./_generated/server";
import { createClerkClient } from '@clerk/backend';

const clerkClient = createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY });

...

export const checkGuess = mutation({
args: { id: v.id("levels"), guessLatitude: v.float64(), guessLongitude: v.float64() },
handler: async(ctx, args) => {
const level = await ctx.db.get(args.id);

if(!level) {
throw new Error("No levels exist");
}

const correctLat = level.latitude;
const correctLng = level.longitude;

const distanceAway = parseInt(haversineDistanceInFeet(correctLat, correctLng, args.guessLatitude, args.guessLongitude).toFixed(0));
// give some leniency to the distance
let lenientDistance = distanceAway - 20;
if (lenientDistance < 0) {
lenientDistance = 0;
}
// If within 250 feet, score increases by distance if outside of 250 feet score = 0
let score = 250 - lenientDistance;
if (score < 0) { // no negative score
score = 0;
}

// Fetch the user from Clerk
const user = await ctx.auth.getUserIdentity();
if(user !== null) {
console.log(user); // THIS IS PRINTING OUT CORRECTLY
await clerkClient.users.updateUser(user.subject, {
publicMetadata: {
score: score,
},
});
}

return {
correctLat,
correctLng,
distanceAway,
score,
}
}
});
import { v } from "convex/values";

import { mutation, query } from "./_generated/server";
import { createClerkClient } from '@clerk/backend';

const clerkClient = createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY });

...

export const checkGuess = mutation({
args: { id: v.id("levels"), guessLatitude: v.float64(), guessLongitude: v.float64() },
handler: async(ctx, args) => {
const level = await ctx.db.get(args.id);

if(!level) {
throw new Error("No levels exist");
}

const correctLat = level.latitude;
const correctLng = level.longitude;

const distanceAway = parseInt(haversineDistanceInFeet(correctLat, correctLng, args.guessLatitude, args.guessLongitude).toFixed(0));
// give some leniency to the distance
let lenientDistance = distanceAway - 20;
if (lenientDistance < 0) {
lenientDistance = 0;
}
// If within 250 feet, score increases by distance if outside of 250 feet score = 0
let score = 250 - lenientDistance;
if (score < 0) { // no negative score
score = 0;
}

// Fetch the user from Clerk
const user = await ctx.auth.getUserIdentity();
if(user !== null) {
console.log(user); // THIS IS PRINTING OUT CORRECTLY
await clerkClient.users.updateUser(user.subject, {
publicMetadata: {
score: score,
},
});
}

return {
correctLat,
correctLng,
distanceAway,
score,
}
}
});
26 replies
CCConvex Community
Created by DylanDev on 10/8/2024 in #support-community
Supposed to return ids in random order but mostly does one image
I'm trying to pull down 5 document entries in my "levels" table and it seems to only ever return the ids in the same order most of the time. Every once and a while and sometimes when I have to refresh the npx convex dev server, will it then pull a different image. Is my code wrong?
import { v } from "convex/values";

import { mutation, query } from "./_generated/server";
import { Doc, Id } from "./_generated/dataModel";

// Gets the number of random Levels from the database
export const getRandomLevels = query({
handler: async (ctx) => {
// TODO: Implement a way to choose number of levels based on backend settings
const numOfLevels = 5;
const levels = await ctx.db.query("levels").collect();

if(levels.length < numOfLevels) {
throw new Error("Not enough levels to complete request!");
}

console.log(levels);

const selectedLevels = [];

for(let i = 0; i < numOfLevels; i++) {
const randomIndex = Math.floor(Math.random() * levels.length);
selectedLevels[i] = levels[randomIndex];
levels.splice(randomIndex, 1);
}

// Extract and return the Ids of the selected levels
const levelIds = selectedLevels.map(level => level._id);
console.log("[SERVER SIDE] Ids", levelIds);
return levelIds;
}
});

export const getImageSrc = query({
args: { id: v.id("levels") },
handler: async (ctx, args) => {
if(!args.id) {
throw new Error("Missing entryId parameter.");
}

const level = await ctx.db.get(args.id);

if(!level) {
throw new Error("No levels exist");
}

const imageUrl = await ctx.storage.getUrl(level.imageId);

return imageUrl;
}
});
import { v } from "convex/values";

import { mutation, query } from "./_generated/server";
import { Doc, Id } from "./_generated/dataModel";

// Gets the number of random Levels from the database
export const getRandomLevels = query({
handler: async (ctx) => {
// TODO: Implement a way to choose number of levels based on backend settings
const numOfLevels = 5;
const levels = await ctx.db.query("levels").collect();

if(levels.length < numOfLevels) {
throw new Error("Not enough levels to complete request!");
}

console.log(levels);

const selectedLevels = [];

for(let i = 0; i < numOfLevels; i++) {
const randomIndex = Math.floor(Math.random() * levels.length);
selectedLevels[i] = levels[randomIndex];
levels.splice(randomIndex, 1);
}

// Extract and return the Ids of the selected levels
const levelIds = selectedLevels.map(level => level._id);
console.log("[SERVER SIDE] Ids", levelIds);
return levelIds;
}
});

export const getImageSrc = query({
args: { id: v.id("levels") },
handler: async (ctx, args) => {
if(!args.id) {
throw new Error("Missing entryId parameter.");
}

const level = await ctx.db.get(args.id);

if(!level) {
throw new Error("No levels exist");
}

const imageUrl = await ctx.storage.getUrl(level.imageId);

return imageUrl;
}
});
7 replies
CCConvex Community
Created by DylanDev on 10/8/2024 in #support-community
API Functions Returning from Server but showing undefined in client
No description
3 replies
CCConvex Community
Created by DylanDev on 10/7/2024 in #support-community
How to find a table entry from _id
I want to query the my table and find the table document that has the _id i pass in. How do I do this?
6 replies
CCConvex Community
Created by DylanDev on 10/5/2024 in #support-community
Help with loading images from image id in a table and image sitting in the Files
No description
5 replies