DDave
DDave

Reputation: 1553

build object and then export it , in js

I used to have an resolvers.js with an object:

export const resolvers = {
   value1: 'value';
   value2: 'value';
}

and then:

import { resolvers } from './graphqlresolvers';

now I need to build my object with variable properties

let resolvers = {}

resolvers.value1= 'value';
resolvers.value2= 'value';

export resolvers;

but i get this error: Unexpected token in export resolvers;

if i do:

export default resolvers;

works ok, but then my app behaviour get crazy.... no erros, but server app is not sending right information to app client.

Always it's problem for me have no freedom to define variables, and then export or not at the end of code. someone can explain me how i must to do?

Upvotes: 4

Views: 39

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1075925

You can add the properties after the export statement:

export const resolvers = {};
resolvers.value1 = 'value';
resolvers.value2 = 'value';

Live on plnkr (requires cutting-edge browser, a'la Chrome v62)

You could even do it if you needed to do the assignment later, because bindings are dynamic:

export let resolvers;
resolvers = {};
resolvers.value1 = 'value';
resolvers.value2 = 'value';

Live on plnkr (requires cutting-edge browser, a'la Chrome v62)

Upvotes: 2

Bergi
Bergi

Reputation: 665594

You are looking for the syntax

export { resolvers as resolvers }

or short

export { resolvers }

to export a variable declared elsewhere. Of course, even when building the object dynamically, you can declare the export together with the variable:

export let resolvers = {};

resolvers.value1= 'value';
resolvers.value2= 'value';

Upvotes: 2

Related Questions