Console.log not working in server actions
Unanswered
Fish Crow posted this in #help-forum
Fish CrowOP
I want to console.log some output in my server action but somehow it just won't print anything in the server terminal.
The client side code gets executed and the server action is also imported. The server action is in a try catch block and when the code fails I get the error: "undefined". Console.log() is working client side but I just don't get any output for server side. I'm looking in the server terminal and not chrome terminal. I even have a console.log statement at the very top of this server action but it still doesn't print anything. Is there a way to debug this? I already took a look at the debug chapter in the docs but this still doesn't help me to troubleshoot it. Was there already an error with server action logging?
The client side code gets executed and the server action is also imported. The server action is in a try catch block and when the code fails I get the error: "undefined". Console.log() is working client side but I just don't get any output for server side. I'm looking in the server terminal and not chrome terminal. I even have a console.log statement at the very top of this server action but it still doesn't print anything. Is there a way to debug this? I already took a look at the debug chapter in the docs but this still doesn't help me to troubleshoot it. Was there already an error with server action logging?
4 Replies
Fish CrowOP
Sure!
Server action function
This action copies an encrypted file from my amazon s3 bucket. Everything important like S3 client and creds are imported.
Server action function
"use server"
...
export const createShareLink = async (fileId: string, shareTime: number, orgOrUser: any) => {
let encryptionKey;
let encryptionKeyMD5;
console.log("Test Output")
if (!orgOrUser?.id) {
encryptionKey = orgOrUser!.publicMetadata?.encryptionKeyBase64 as string;
encryptionKeyMD5 = orgOrUser!.publicMetadata?.encryptionKeyMD5Base64 as string;
} else {
encryptionKey = orgOrUser?.publicMetadata?.encryptionKeyBase64 as string;
encryptionKeyMD5 = orgOrUser?.publicMetadata?.encryptionKeyMD5Base64 as string;
}
console.log("See if user has access")
const access = await hasAccessToFile(fileId);
console.log("Hello from createShareLink")
if (!access) {
throw new Error("Not authorized");
}
// Copy the file
const copyParams = {
Bucket: censored,
CopySource:censored/${fileId},
Key: fileId,
CopySourceSSECustomerAlgorithm: encryptionKey && 'AES256',
CopySourceSSECustomerKey: encryptionKey && encryptionKey,
CopySourceSSECustomerKeyMD5: encryptionKeyMD5 && encryptionKeyMD5,
}
await s3Client.send(new CopyObjectCommand(copyParams))
const presignedUrl = await getSignedUrl(s3Client, new GetObjectCommand({
Bucket: censored,
Key: fileId,
}), { expiresIn: shareTime });
return presignedUrl
}This action copies an encrypted file from my amazon s3 bucket. Everything important like S3 client and creds are imported.
This is the client side component, it triggers my action when I submit a form:
The console.log() does work on the client side till console.log("This is the orgOrUser: ", orgOrUser). Then I just don't get any response and the try catch block is just returning "undefined". No console.log() statement is working on the server side from the action.
All imports are also fine!
async function onSubmit(values: z.infer<typeof formSchema>) {
if (!orgId) return;
try {
console.log("Meooowwww")
const daysInSeconds = parseInt(form.getValues("days")) * 24 * 60 * 60;
const shareUrlId = await createShareUrl(daysInSeconds);
console.log("Share URL ID: ", shareUrlId)
setIsShared(true);
setShareUrl(shareUrlId || '');
toast({
variant: "success",
title: "File Uploaded",
description: "Your file has been successfully shared",
});
} catch (error) {
toast({
variant: "destructive",
title: "Something went wrong",
description:Your file could not be shared. Please try again later. ${error},
});
}
}
const createShareUrl = async (shareTime: number) => {
if (!orgId) {
return null;
}
console.log("Creating share link")
console.log("This is the orgOrUser: ", orgOrUser)
const shareUrl = await createShareLink(fileId, shareTime, orgOrUser);
console.log("Share URL: ", shareUrl)
return shareUrl
const formData = new FormData();
await formData.append('targetLink', shareUrl as string);
const res = await fetch('/share/generate', {
method: 'POST',
body: formData
}).then(res => res.json())
return res.shareUrl as string || '';
};The console.log() does work on the client side till console.log("This is the orgOrUser: ", orgOrUser). Then I just don't get any response and the try catch block is just returning "undefined". No console.log() statement is working on the server side from the action.
All imports are also fine!
Fish CrowOP
Found the issue. The problem was that I tried to pass an object to the server action which is not working. I just parsed the important string and just passed the string instead of the whole object.