Reputation: 11
When using NestJS REPL I'm getting the following error despite running against an async function that I can see is returning a Promise.
Uncaught SyntaxError: await is only valid in async function
See the block below for the Service code I'm calling and an example of the code returning a promise but then throwing the error when trying to await.
CoffeesService.ts
async findOne(id: string) {
const coffee = await this.coffeeRepository.findOne(id, {
relations: ['flavors'],
});
if (!coffee) {
throw new NotFoundException(`Coffee ${id} not found`);
}
return coffee;
}
REPL
> coffeesService.findOne(1)
Promise {
<pending>,
[Symbol(async_id_symbol)]: 393,
[Symbol(trigger_async_id_symbol)]: 45,
[Symbol(destroyed)]: { destroyed: false }
}
> await coffeesService.findOne(1)
await coffeesService.findOne(1)
^^^^^
Uncaught SyntaxError: await is only valid in async function
According to NestJS docs, await is a top level function https://docs.nestjs.com/recipes/repl#:~:text=Native%20functions-,Read%2DEval%2DPrint%2DLoop%20(REPL),%7D%0A%3E%20await%20appController.getHello()%0A%27Hello%20World!%27,-To%20display%20all
This is a great new feature and I'm hoping I'm just doing something wrong :/
Upvotes: 0
Views: 330
Reputation: 11
As suggested above, I needed to upgrade past node version 16. it works perfectly in node 18
> await get(CoffeesService).findOne(1)
Coffee {
id: 1,
name: 'Hobo roast',
brand: 'SF Roasters',
description: null,
recommendations: 0,
flavors: [
Flavor { id: 1, name: 'trash' },
Flavor { id: 2, name: 'cigarettes' }
]
}
Upvotes: 1