Not getting fileURL as expected

Question:
Does getFileInfo is working?

What do I want to achieve?
I’m trying to know the static URL of a file that a user has uploaded. I’m using uploadFiles()
function to allow users upload a pdf document, then I’m trying to retrieve the URL that MediaManager created for that File.

For that, I’m using back end functions getFileInfo and getFileUrl, but I’m receiving nothing as response. I checked that uploadFiles() function is running as expected and it is, I have the uploaded files in Visitor Uploads

What have you already tried:

$w.onReady(function () {

$w('#uploadButton1').fileType = "Document"
$w('#button79').onClick(() =>{
  $w('#uploadButton1').startUpload()
  .then( (uploadedFile) => {
    let url = uploadedFile.url; 
    let urlInfo = fileInfo(url)
     console.log(url)
     return Promise.all(urlInfo)
  } )
  .catch( (uploadError) => {
    let errCode = uploadError.errorCode;  // 7751
    let errDesc = uploadError.errorDescription; // "Error description"
    console.log(errCode)
    console.log(errDesc)
  });

}) 
$w("#upload").fileType = "Document"; // Site visitor can choose an image 
$w('#uploadFile').onClick( () => {     
  console.log($w('#upload').value)            
  if ($w("#upload").value.length > 0) {  // Site visitor chose a file
    $w("#upload").uploadFiles()
      .then( (uploadedFiles) => {
        uploadedFiles.forEach(uploadedFile => {
        let fileUrl = uploadedFile.fileUrl
        
        return fileInfo(fileUrl)
        })
      console.log("Upload successful.");
      } )
      .catch( (uploadError) => {
      console.log("File upload error: " + uploadError.errorCode);
      console.log(uploadError.errorDescription);
      } );
  }
  else {  // Site visitor clicked button but didn't choose a file
    console.log("Please choose a file to upload.")
  }
} );

import { mediaManager } from 'wix-media-backend';


export async function fileInfo(file) {
  try {
    console.log("Before calling mediaManager.getFileUrl");
    const result = await mediaManager.getFileInfo(file);
    console.log(result);
  } catch (error) {
    console.error("Error getting file info:", error);
  }
}

I do not see the IMPORTS of your BACKEND-FUNCTIONS on your FRONTEND.
Where are the IMPORTS ? Did you show the whole related code ?

This part of code is surely running on your backend-code? Or did you place that part also on your FRONTEND, like shown in your example? If so → it is wrong!

Did you create a JSW-Backend-file already ?

Seems like you are mixing up something, or did not show complete code.

IMPORTS are always on the very top of your page’s code (no matter if frontend or backend-coding)

Hi! Yes I’m importing my functions from the back-end. Srry if I didn’t separate the code in this example. And as you mentioned, this is the whole related code

Can you imagine where’s the mistake ?

NOPE !!! You are not !!!

Take a closer look onto this example…

//############ MISSING IMPORT OF THE fileInfo-FUNCTION FROM THE BACKEND HERE !!!!!! ##############################
//import { fileInfo } from 'backend/YOUR_JSW_MODULE_NAME_HERE'; //--> in this example xyz.jsw;

import { fileInfo } from 'backend/xyz.jsw'; //<--- now you have imported your exported function to your frontend and are able to use it!!!

$w.onReady(()=> {
	$w('#uploadButton1').fileType = "Document";	
	$w('#button79').onClick(() => {console.log('clicked');
		$w('#uploadButton1').startUpload()
		.then(async(uploadedFile) => {console.log("UPLOADED-FILE: ", uploadedFile);
			let url = uploadedFile.url; console.log("URL :", url)
			let urlInfo = await fileInfo(url); console.log("URL-INFO: ", urlInfo);
		})
		.catch((uploadError)=> {
			let errCode = uploadError.errorCode; console.log("Error-Code: ", errCode)
			let errDesc = uploadError.errorDescription; console.log("Error-Descrpt: " ,errDesc);				
		});
	});

	$w("#upload").fileType = "Document"; 
	$w('#uploadFile').onClick(()=> {console.log($w('#upload').value);	
		if ($w("#upload").value.length > 0) { // Site visitor chose a file
			$w("#upload").uploadFiles()
				.then((uploadedFiles)=> {
					uploadedFiles.forEach(async(uploadedFile)=> {console.log("UPLOADED-FILE: ", uploadedFile);
						let fileUrl = uploadedFile.fileUrl; console.log("FILE-URL :", fileUrl)
						let urlInfo = await  fileInfo(fileUrl); console.log("URL-INFO: ", urlInfo);
					});	
					console.log("Upload successful.");
				})
				.catch((uploadError)=> {
					console.log("File upload error: " + uploadError.errorCode);
					console.log(uploadError.errorDescription);
				});
		} else {console.log("Please choose a file to upload.");}
	});
}); //<---- was missing here! ! !!


//-------------- [ BACKEND-CODE ]------------------
//This code-part should be inside an JSW-Backend-module in this example ---> xyz.jsw !!!!
import { mediaManager } from 'wix-media-backend';

export async function fileInfo(file) {
	try {
		const result = await mediaManager.getFileInfo(file); 		
		return result;
	} catch (error) {
		console.error("Error getting file info:", error);
	}
}

Go through all your code and check it for ERRORS and SYNTAX-ERRORS first !
Do you use the CONSOLE ? Add more CONSOLE-LOGS to your code → and inspect step by step process. Where does the code stops?

Code still not looks perfectly! I did not test it, so it still could have some bugs.
Use the logs, it will help you out !

I expressed myself incorrectly Russina-dima! So sorry! As you pointed in my original code I have already imported my back end module / function and the code stills not working! I have a console .loge inside both functions and when I use getFileInfo(file) the log is undefined, and using getFileUrl(file) instead getFileInfo(file) the log don’t exists.

Always remember that the file is getting uploaded correctly into my MediaManager. The code never stops, just I’m not getting the expected retrieved info

Any ideas?

  1. show me your BACKEND-MODULE on a pic…
  2. Show me your full frontend-code (separately).
  3. Show me your full BACKEND-CODE (separatelly).

Thanks! Very grateful with your support!

Backend-module:

Frontend-code:

Here there are both codes! I changed the getFileInfo() and getFileUrl() functions to listFolders() folders function, in order to get: Folder’s ID and then retrieve the files url’s and information. I got inspired from another forum, but i’m not getting any console log. The files are uploading correctly

Source: Create a URL for a media file: the truth about getFileURL - #14 by polkaset

Ok, this already looks better and makes more sense.

What do we have ?

  1. BACKEND → app.jsw
  2. unused → pagination
  3. global filters → undefined/empty
  4. your Export-function → myListFoldersFunction
  5. using ASYNCHRONOUS-METHOD → good!

Hmmm, let’s compare with the ORIGINAL API-CODE…

import { mediaManager } from 'wix-media-backend';

const filters = {
  parentFolderId: "8a3be85ea03e4b8b82f2f9c989557c3d"
};

export function myListFoldersFunction() {
  return mediaManager.listFolders(filters, null, null)
    .then((myFolders) => {
      const folderName = myFolders[0].folderName;
      const updatedDate = myFolders[1]._updatedDate;
      return myFolders;
    })
    .catch((error) => {
      console.error(error);
    });
}

Where is the difference?

  1. Well you are using a function for → List folders in a specified folder
    …but you never mention the ID of a specific folder…
const filters = {
  parentFolderId: "8a3be85ea03e4b8b82f2f9c989557c3d"
};
  1. And even when not mentioning a specific folder…

—> then ----> (null, null, null) ??? <— already tried it out ?

  1. And what about…
import { mediaManager } from 'wix-media-backend';

export function myListFoldersFunction() {
  return mediaManager.listFolders()
    .then((myFolders) => {
      const folderName = myFolders[0].folderName;
      const updatedDate = myFolders[1]._updatedDate;
      return myFolders;
    })
    .catch((error) => {
      console.error(error);
    });
}

—> to list folders in the root folder ???

On Frontend → maybe improve first line →
import {myListFoldersFunction} from ‘backend/app.jsw’

And why you are calling → myListFoldersFunction in the second line of your code , when your page is not even ready at that moment ?

You are not using ASYNCHRONOUS-FUNCTIONS on your frontend → to await for the results from BACKEND! → which i already have shown in my first code-correction.
That means, you are not waiting for results.

This maybe was not a good advice—> —> then ----> (null, null, null ) ??? <— already tried it out ?

But try out anyway.

If you are still not able to get it to work, i will have to recreate your issue and test it on my own.

USE MORE THE CONSOLE-LOGS!!! → They will help you to understand what’s going on.

REQUEST-REPLY-CONSOLE-DEBUG-METHOD

AND ALMOST FORGOT → NEXT TIME DO NOT SHOW CODE ON A PIC!
CODES —> ALWAYS AS TEXT PLEASE!

NO NEED TO RETYPE ALL THE CODE MANUALLY AGAIN.

You are a genius! It worked perfectly! Thanks very much. Having this open forum, I would like to ask you: Do you know how can I upload files into a collection?

1 Like

Check the APIs for this process first.
If no APIs for this process existing → then it gets very very tricky, or even impossible to do that.

CHECK-APIs here first…

Still not completely sure about all the V2.APIs, if there are already useable or not → but check anyway…

https://www.wix.com/velo/reference/wix-media-v2?utm_source=google&utm_medium=cpc&utm_campaign=13708482663^124757113632&experiment_id=^^530755701290^^_DSA&gclid=CjwKCAiA9ourBhAVEiwA3L5RFpsxQKg5MkGFuD_qfWEBWkViJtfFRP2wP_4FfOS8CgHTWq46hLWSyBoCDfwQAvD_BwE

And always try to work on backend → more possibilities and faster.

Don’t forget to mark as RESOLVED.

I like it when you like it :wink:

If it comes to work with wix-data-manager…may be the following and all it’s connected/related posts could be interessting for you aswell…

I just realized that you wrote into a collection! → Do you mean into your DATABASE ?

Yes, it should be possible.