0

I'v just deployed my api on my server, it worked great until i used the spread syntax. Which now gives me back this error :

(node:17373) UnhandledPromiseRejectionWarning: /home/admin/api/src/graphql/index.js:15
      { ...accChild, [currKey]: {
        ^^^

SyntaxError: Unexpected token ...
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
(node:17373) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17373) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any ideas ?

So the code is working locally and this is the whole line :

let resolvers = resolversExport.reduce((acc, curr) => {
    let currArray = require(`./resolvers/${curr}`);
    return Object.keys(currArray).reduce((accChild, currKey) => 
      { ...accChild, [currKey]: { 
        ...(accChild[currKey] || {}), 
        ...currArray[currKey]} 
    },
    acc);
  }, {});

FIXED Look at the comments, {} must be wrapped around () in arrow functions. (i will delete this if someone wants to put it as an official answer)

let resolvers = resolversExport.reduce((acc, curr) => {
        let currArray = require(`./resolvers/${curr}`);
        return Object.keys(currArray).reduce((accChild, currKey) => 
          ({ ...accChild, [currKey]: { 
            ...(accChild[currKey] || {}), 
            ...currArray[currKey]}) 
        },
        acc);
      }, {});
Bidoubiwa
  • 910
  • 2
  • 12
  • 25
  • Can you post the code in addition to the error? – Steven Mays Mar 20 '19 at 14:17
  • Rest element must be last element – ponury-kostek Mar 20 '19 at 14:18
  • 1
    Add `()` around `{ ...accChild, [currKey]: { ...(accChild[currKey] || {}), ...currArray[currKey]} ` – ponury-kostek Mar 20 '19 at 14:20
  • @ponury-kostek it is, i'v updated the code you can check, >adiga yes i could but i would like to know how to fix this problem >Steven Mays it's updated ! – Bidoubiwa Mar 20 '19 at 14:20
  • 1
    You need to add `()` as @ponury-kostek suggested. It's considering `{}` as a code block – adiga Mar 20 '19 at 14:21
  • I did it work thanks ! – Bidoubiwa Mar 20 '19 at 14:21
  • 4
    Possible duplicate of [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript-6-arrow-function-that-returns-an-object) and ['Unexpected token' syntax error in object returned by arrow function](https://stackoverflow.com/questions/34699528) – adiga Mar 20 '19 at 14:22

0 Answers0