Using Node Modules on the Backend?

Hey!

I’ve been using the Dropbox Javascript SDK on the front-end. However, once I try to use it on the back-end in a web module, it keeps throwing errors. I was wondering if an SDK/API like this can be used on the backend on its own, or do I need to use Fetch.

Thanks,
Tanush

Have you installed the dropbox NPM?

Yea. I changed up my strategy a bit and I’m now using the fetch function. I seem to be having an issue here. Here’s my backend code. I know the fetch is working perfectly, and I know that I’m getting the URL string as wanted (I confirmed with Dropbox support). The main issue seems to be happening on the front-end though. I’m trying to store this returned value by doing such:

let requestURL = generateSubmission();
// i then try to set the .text property of a text field to this requestURL variable

I think I’m doing something wrong with how I call the function on the front end, because the Web Console has an error about how I’m trying to store a Promise where I should have a string.

**BACKEND CODE:**
export function generateSubmission() {
 let generated;
 const params = {
 //private stuff
    }


 let headers = {
 //private stuff I'd rather not show
    };



 let options = {
        method: 'POST',
        headers: headers,
        body: params
    };

    fetch('https://api.dropboxapi.com/2/file_requests/create', options)
    .then(res => {
 let response = res.json();
        generated = response["url"];
    })
 return generated;

}

you’re not returning the functions correctly:

  1. return fetch ( ‘https://api.dropboxapi.com/2/file_requests/create’ , options )

    • the return generated; should be inside the .then() block.

@jonatandor35 What’s the purpose of returning the fetch statement as whole?

  • You are missing another thing:
//....
return  fetch('https://api.dropboxapi.com/2/file_requests/create', options)
    .then(res => {
return res.json();
    }).then(response => {
      generated = response["url"];
	return generated;
})
.catch(err => err);

You need to return every promise in the promise chain as you can see here.

@jonatandor35 Oh. That makes so much more sense now, thanks!

But now I’m returning a promise chain, so how should I change my front-end code to be able to access the value stored in generated?

@tanushyadav first, be sure to put your backed code in a .jsw file and not in a .js file.
Second, on your front en import the module like:

import {generateSubmission} from 'backend/dropbox.jsw';
generateSubmission()
.then(requestURL  => {
console.log(requestURL);//or do whatever you like with this result
})