"Type Error - .then is not a function" error in workflow

This topic was automatically generated from Slack. You can find the original thread here.

Evyn Ecclesia : RE: TYPE ERROR .then is not a function (node js)

How can I get this statement to run:
.then((ret) => console.log(ret))

I keep getting “.then” is not a function in node js.

Dylan Sather (Pipedream) : Hi , could you share your full code?

Generally this error means that the variable you’re trying to run the then method on isn’t a Promise.

I’d also suggest using async / await syntax to retrieve the results of Promises. It’ll simplify your code and is generally easier to reason through: https://www.smashingmagazine.com/2020/11/comparison-async-await-versus-then-catch/ .

Evyn Ecclesia : Hi Dylan,

I’ve had time to digest documentation.

I’m still getting “keyy.then” is not a function.

I’d be happy to attach my code (as well as Fauna’s) for your comparison and review.

Dylan Sather (Pipedream) : Thanks. Instead of the keyy.then... clause, can you run

console.log(keyy)

and see if the value of keyy contains your query results?

Notice how the Fauna docs don’t have an await before the client.query clause. client.query returns a Promise, which has a then method you can run to retrieve the results of the Promise.

Since you do include an await before the client.query clause (which is recommended), you don’t need to run then on the results - keyy should contain your results. await pauses the execution of the code and returns the resolved value without needing to run .then. So these two are equivalent:

const keyy = await client.query(...)

// keyy contains results
console.log(keyy)
const keyyPromise = client.query(...)

keyyPromise.then(keyy => {
  // keyy contains results
  console.log(keyy)
})

That article I shared on async / await syntax explains this a bit more deeply. This is also a good overview of asynchronous programming in JS in general. Let me know if that helps.

Evyn Ecclesia : Ok. Thanks much Dylan!!! :thumbsup:

I chose your first option, and it worked like a charm.

WHEW! Fauna Docs were truly sending me down Bunny Trails & Rabbit Holes :thinking_face:.

Dylan Sather (Pipedream) : Yeah async / await syntax is much easier to read than the then method chaining. This stuff is hard to grok - took me years of trial and error.