Problem with sending POST/GET Request at the same time
Now I'm looking to load the last projects of a user from the database by sending his name and receiving all the last projects related to this user. For some reason, I keep getting "500 Internal Server Error" everytime I try to do this.
the frontend code:
let lastProjects = ref(null);
async function getLastProjects () {
const { response } = await $fetch('/api/lastProject'
, {
method: 'post',
body: {
identity: data._value.name
},
headers: { 'Content-Type': 'application/json' },
}
)
console.log('response: ', response)
if (!response) {
throw new Error('Response is undefined');
}
lastProjects.value = response;
return lastProjects;
}
onMounted(async () => {
await getLastProjects()
});
backend "/api/lastProject.post.ts":
import { MongoClient } from 'mongodb';
export default defineEventHandler(async (event) => {
try {
const identity = await readBody(event);
const url = process.env.MONGODB_URI;
if(!url) {
throw createError({
status: 400,
message: "there is an error with the url"
});
}
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
const db = client.db();
const collection = db.collection('topicalMapsData');
const users = await collection.findOne({ identity });
if (!users) {
throw new Error('User not found');
}
return users;
} catch(err){
throw new Error('Error: ', err);
}
finally {
await client.close()
}
});
So what's exactly wrong with this code?
1 Reply
try console.log every line and see when it doesn't that's the problem