2

Here is what I have already:

myFunct({ myObj: { db } })

I need to add another function in such as:

myFunct({ myObj: async ({ req }) => {
  //more scripts
} })

What I tried and failed:

myFunct({ myObj: {
  db,
  async (req) => {
    //more scripts
  }
} })

At the => I get the syntax error:

Unexpected token, expected {
amaster
  • 1,915
  • 5
  • 25
  • 51
  • 2
    It's not working since you are not naming the property that will hold the function. The `{ db }` syntax is a shorthand to declare properties with the same name as the identifier is used as value, e.g.: `{ db: db }`. – Christian C. Salvadó Dec 06 '19 at 16:52
  • I knew it would be something I was easily overlooking. Trying to switch my programming language from php to node has came with its own set of challenges. – amaster Dec 06 '19 at 16:54

2 Answers2

3

You have to supply a property name.

If you have a variable, it can act as both the property name and value.

const myFunction = async (req) => {
    //more scripts
};

myFunct({ myObj: {
  db,
  myFunction
} })

If you only have a value, then you need to state the property name explicitly.

myFunct({ myObj: {
  db,
  myFunction: async (req) => {
    //more scripts
  }
} })
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Or alternatively to the second example, `async myFunction (req) { ... }`, though `this` will behave differently. – Patrick Roberts Dec 06 '19 at 16:56
  • @PatrickRoberts how so differently? Being new to async-await, can you direct me to some explanation? – amaster Dec 06 '19 at 16:58
  • 2
    @amaster — https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable – Quentin Dec 06 '19 at 16:59
3

You did not give a key to your function :

Try :

myFunct({
  myObj: {
    db,
    yourKey: async (req) => {
      //more scripts
    }
  }
})
JeromeBu
  • 1,099
  • 7
  • 13