cropsychooo
cropsychooo
TTCTheo's Typesafe Cult
Created by cropsychooo on 8/13/2023 in #questions
Prisma and mySQL 'POST' method 401 Unauthorized
I have Prisma synced with mysql database (mariaDB). I have a "dashboard" that I can access with auth through discord. There I have a form that does POST (and prisma.[whatever].create()) method on submit, but I am getting a 401 error Unauthorized. Just for reference, fetching data with GET and doing prisma.[whatever].findMany(); works normally I also can access and see tables through localhost:3000/api/[whatever] I am assuming that there is no session data available on request since I am getting this in terminal when I try to submit:
https://next-auth.js.org/errors#client_fetch_error undefined {
error: {},
url: 'http://localhost:3000/api/auth/session',
message: undefined
}
https://next-auth.js.org/errors#client_fetch_error undefined {
error: {},
url: 'http://localhost:3000/api/auth/session',
message: undefined
}
But I dont understand how is there no session when I use getSession... this is the server file in pages
import { prisma } from "~/server/db";
import { getSession } from "next-auth/react";
import { PrismaClient } from '@prisma/client';

export default async function handler(req, res) {
const session = await getSession({ req });

if (req.method === "POST") {
if (!session) {
console.log('no session')
return res.status(401).json({ error: "Not authenticated" });
}

// Extract post data from request body
const { title, content } = req.body;

try {
// Create a new post in the database
const post = await prisma.post.create({
data: {
title,
content,
authorId: session.user.id, // session from next-auth
},
});

return res.status(201).json(post);
} catch (error) {
return res.status(500).json({ error: "Failed to create post." });
}
}
import { prisma } from "~/server/db";
import { getSession } from "next-auth/react";
import { PrismaClient } from '@prisma/client';

export default async function handler(req, res) {
const session = await getSession({ req });

if (req.method === "POST") {
if (!session) {
console.log('no session')
return res.status(401).json({ error: "Not authenticated" });
}

// Extract post data from request body
const { title, content } = req.body;

try {
// Create a new post in the database
const post = await prisma.post.create({
data: {
title,
content,
authorId: session.user.id, // session from next-auth
},
});

return res.status(201).json(post);
} catch (error) {
return res.status(500).json({ error: "Failed to create post." });
}
}
Please, for the love of god, does anyone know where the problem is? I am going crazy by not finiding ways out of this. Thank you
9 replies
TTCTheo's Typesafe Cult
Created by cropsychooo on 11/27/2022 in #questions
file.json.gz - SyntaxError unexpected end of data at line 1 column 1 of the JSON data
Hi guys, first I want to apologize if this question is extremely stupid but I have no clue how would I even begin to solve this issue. Anyway, I am fetching a file export from The Movie DB. http://files.tmdb.org/p/exports/movie_ids_11_22_2022.json.gz If you open this file you will see that its a json with objects that are not seperated with commas, therefor I can not "extract" stuff out of them. I am doing all of this in React with package called Pako which essentially handles the zip, right. code:
useEffect(() => {
async function exec(i = 0) {
console.group('file: ', i);
try {

const res = await fetch(urls[i], {
mode: 'no-cors'
});
// convert to arrayBuffer for further processing
const buf = await res.arrayBuffer();
// or get blob using `await res.blob()`
// and convert blob to arrayBuffer using `await blob.arrayBuffer()`

console.log('input size: ', buf.byteLength);

// decompress file
const outBuf = pako.inflate(buf);
// console.log('output size: ', outBuf.byteLength);

// convert arrayBuffer to string
const str = new TextDecoder().decode(outBuf);
// console.log('json string', str);

// print json object
console.log('json object', JSON.parse(str));
} catch (err) {
console.error('unable to decompress', err);
}
console.groupEnd('file: ', i);
}

async function init() {
for (let i in urls) await exec(i)
}
init();

},[])
useEffect(() => {
async function exec(i = 0) {
console.group('file: ', i);
try {

const res = await fetch(urls[i], {
mode: 'no-cors'
});
// convert to arrayBuffer for further processing
const buf = await res.arrayBuffer();
// or get blob using `await res.blob()`
// and convert blob to arrayBuffer using `await blob.arrayBuffer()`

console.log('input size: ', buf.byteLength);

// decompress file
const outBuf = pako.inflate(buf);
// console.log('output size: ', outBuf.byteLength);

// convert arrayBuffer to string
const str = new TextDecoder().decode(outBuf);
// console.log('json string', str);

// print json object
console.log('json object', JSON.parse(str));
} catch (err) {
console.error('unable to decompress', err);
}
console.groupEnd('file: ', i);
}

async function init() {
for (let i in urls) await exec(i)
}
init();

},[])
Do you have any idea how to seperate all of those objects with commas so I can read stuff out of them? Its my first question here so if there is a code formatter sorry for not figuring out where it is 😄 And thanks!!
36 replies