1

This is a very easy question but no google search results return the correct answer.

const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
const poolec2 = require("./db");
require("./function")();

returnMappings = async function(connection){
  try {
    let mapping = await connection.query("SELECT ticker FROM mappings");
    let results = await mapping.rows;
    //console.log(results);
    return results;
  } catch (err) {
    console.error(err.message);
  }
};

const mappings = returnMappings(poolec2);
console.log(mappings);

What am I missing here that is not returning my data? When I unquote console.log(results); I can see the desired results in my terminal. I've tried various versions of using .then but have not had any success return results.then;, const mappings = returnMappings(poolec2).then;, console.log(mappings.then);. I've also tried returning my results outside of my try catch with no luck. I'm really stuck trying to understand how I go about returning a from an async function.

EDIT

The goal is to pass the results from the above async function to another function to check if the a user inputted value exists in that vector of mappings. So indexOf based on a user input, I then use if else to return true or false. With the final results being either true or false.

checkMappings = function(string,input){
  stringArray = string;
  value = stringArray.indexOf(input);
  if(value > -1){
      return false
  }else{
      return true
  }
};

SOLUTION

returnMappings = async function(connection,input){
  try {
    const mapping = await connection.query("SELECT ticker FROM mappings_production");
    const results = await mapping.rows;
    //console.log(results);
    return results;
  } catch (err) {
    console.error(err.message);
  }
};

checkMappings = function(string,input){
  let stringArray = JSON.stringify(string);
  let value = stringArrayC1.indexOf(input);
  function test(a) {
    let check;
    if(a > -1) {
      return true
    }else {
      return false
    }
  };
  console.log(test(value));
  return test(value);
};

const resMappingCheck = returnMappings(poolec2).then((mappings) => checkMappings(mappings,"AAPL"));

console.log(resMappingCheck);

this worked for what I needed to do

2 Answers 2

2

As others have pointed out, await can only be used in an async function, but using .then() is functionally equivalent.

This syntax that should work for you:

returnMappings(poolec2).then((mappings) => console.log(mappings));

if you want to do something more elaborate / multi-line, you can use curly braces like so:

returnMappings(poolec2).then((mappings) => {
  console.log(mappings)
});

UPDATE:

If you want to chain two functions together, then you'll need to start with the .then() pattern: you can declare the callback function in .then() to be asynchronous. At that point, you can start to use await like you're used to.

I'm not sure what relationship you're trying to create between returnMappings() and checkMappings(), but you can chain them together like this: (note the use of async on the first line to allow the use of await inside the callback.)

returnMappings('test').then(async (mapping) => {
  const checkResult = await checkMappings(mapping)
  console.log(`checkMapping result: ${checkResult}`)
}).catch((err) => console.log(`Error: ${err}`))
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried this and I'm given the error message await is only valid in async function
@S3AN556 As the error message says, you can only use await in an async function.
Your first line of code does log the correct data. How can I store that data to be used in another function? Or do I need to do all of this in the same function, I've added more context above.
@S3AN556 I've updated my answer to elaborate; let me know if you have any other questions.
2

Try this:

const mappings = await returnMappings(poolec2);

That will work if you wrap the code inside an async function. Or you could do:

let mappings;
returnMappings(poolec2).then(res => {
    mappings = res;
});

2 Comments

I think I'm understanding some of what you and others are saying, I need to call returnMappings within an async function...just as my error message tells me to. See my attempt at a solution above, I've added an additional explanation.
You got it. Do what your error messages tell you to ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.