AnswerBun.com

toUpperCaseFunction does not work with await keyword

Stack Overflow Asked by goxarad784 on December 18, 2020

I was trying to use an if clause like this:

if(await database.getValue("SDA").toUpperCase() == "AP"){
   ...

I got an error:

TypeError: database.getValue(…).toUpperCase is not a
function

However when I change the above if clause to

let check = await database.getValue("SDA")
if(check.toUpperCase() == "AP"){
  .....

It works fine.

Why does this happen in Nodejs?

2 Answers

Property access (.) has higher operator precedence (20) than await (17). Your current code is equivalent to:

if(await (database.getValue("SDA").toUpperCase()) == "AP"){

because the .s get chained together before the await tries to resolve the expression on the right - which doesn't work because the getValue call returns the Promise.

You could also fix it by grouping the await getValue call in parentheses:

if((await database.getValue("SDA")).toUpperCase() == "AP"){

Or by using a regular expression to get around the == operator precedence (see revision history, I wouldn't recommend it, it's very weird).

Personally, I'd prefer your original code of extracting it into a variable first, it's a bit more readable

Correct answer by CertainPerformance on December 18, 2020

try this shape if((await database.getValue("SDA")).toUpperCase() == "AP"){} or refactor code like this

   try {
  const value = await database.getValue("SDA")
  if(!value) return // what you ned
  if (value === 'AP'){
    // do thomthing 
  }

} catch (e) {

// handle with error 
}

Answered by Mohammed_Alreai on December 18, 2020

Add your own answers!

Related Questions

NGINX gives 502 error from browsers but not from Postman

1  Asked on October 13, 2020 by knguyen

   

Python Pandas cumsum with shift of n

1  Asked on October 10, 2020 by gonzalo-polo

     

How to read only half a byte from a binary file at a time in C?

2  Asked on October 9, 2020 by nguyenvivian

         

Failed to install laravel/jetstream on laravel 8

2  Asked on October 8, 2020 by vldcndn

     

liquibase roll back with spring boot

1  Asked on October 7, 2020 by user1999453

       

How to change the order of jquery events?

0  Asked on October 2, 2020 by turqay-umudzade

   

How to == async axios value

0  Asked on September 30, 2020 by jessica-bulldog

   

Is there a way to set variable methods in Python?

1  Asked on September 30, 2020 by defarm

     

How to pull data from an array using SQL

1  Asked on September 29, 2020 by sarah

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP