sql query issue
Unanswered
plz mention when u reply :3 posted this in #help-forum
hey! ^^
i am trying to fetch data from my vercel postgres database and am having some issues with writing the correct query.
here you go my
so, a sample api request will look like
but if i log this query and copy, then use it (as in the comment), the code runs purrfectly normally. i can't understand why the same code fails when it includes template strings and just runs when it doesn't.
i'd like to know what i'm doing wrong here. thanks! :3
i am trying to fetch data from my vercel postgres database and am having some issues with writing the correct query.
here you go my
/api/shorten/route.ts:import { sql } from '@vercel/postgres'
import { NextRequest, NextResponse } from 'next/server'
export async function GET(req: NextRequest) {
const { searchParams } = req.nextUrl,
codes = searchParams.getAll('code'),
links = await sql`SELECT * FROM urls WHERE code IN ('${codes.join(`', '`)}')`
console.log(`SELECT * FROM urls WHERE code IN ('${codes.join(`', '`)}')`) // returns: SELECT * FROM urls WHERE code IN ('bx1bwgms', 'phhh3jd5')
return NextResponse.json(links.rows)
}so, a sample api request will look like
/api/get?code=foo&code=bar and i get an array of the query as ['foo', 'bar'] in the codes variable. then i convert it to a string (using the .join() method) that looks like ('foo', 'bar') to include in the sql query of the links variable. however, when i run the query, it returns an error:ERROR: bind message supplies 1 parameters, but prepared statement "" requires 0but if i log this query and copy, then use it (as in the comment), the code runs purrfectly normally. i can't understand why the same code fails when it includes template strings and just runs when it doesn't.
i'd like to know what i'm doing wrong here. thanks! :3
5 Replies
so just to check, you use this function by using fetch on client with said url?
@riský so just to check, you use this function by using fetch on client with said url?
oh sorry, didn't see your msg
yes, i do
on the client side, i send a request to
/api/shorten?url=example.com and it should return the value that's been saved to the dbso i can save it in the local storage (i might switch to cookies later) to show it to the user