Cyrus the Great
Cyrus the Great

Reputation: 5922

Return object in async and await in nodejs

I am new in Node.js, I have async method called

async function makeLineStringFromPoints(user) {
    ...

    for (let item of linesGeoJson) {
        saveLineIntoDb({
            'line': item,
            'date': user.date_created,
            'user_id': user.uid,
            'device_id': user.devid,
        }).then((userStored) => {
            console.log(userStored); // i received undefined  
        }).catch((err) => {
            console.log(err);
        });
    }
    ...
}

in this method I have invoke other async method saveLineIntoDb in this method I an storing user information in database :

async function saveLineIntoDb(user) {
    let userStored = user;
    try {
        await db.result(pgp.helpers.insert(user, cs))
            .then(data => {
                return await (user); // return user
            });
    } catch (error) {
        logger.error('saveIntoDatabase Error:', error);
    }
}

Now after storing I want to return user in saveLineIntoDb(user) to }).then((userStored) but always I got undefined.

How can I do that ?

Upvotes: 2

Views: 5524

Answers (3)

Estus Flask
Estus Flask

Reputation: 222309

saveLineIntoDb doesn't return the result and mixes async and raw promises. It also prevents errors from being handled in caller function. return await (user) is excessive, it also won't work because it happens inside regular function.

It should be:

async function saveLineIntoDb(user) {
   await db.result(pgp.helpers.insert(user, cs));
   return user;
}

While caller function doesn't chain promises and doesn't make use of await. If DB queries should be performed in series, it should be:

async function makeLineStringFromPoints(user) {
  ...
  try {
    for (let item of linesGeoJson) {
      const userStored = await saveLineIntoDb(...);
      console.log(userStored);
    }
  } catch (err) {
    console.log(err);
  }
}

If DB queries should be performed in parallel, it should be:

async function makeLineStringFromPoints(user) {
  ...
  try {
    await Promise.all(linesGeoJson.map(async (item) => {
      const userStored = await saveLineIntoDb(...));
      console.log(userStored);
    }));
  } catch (err) {
    console.log(err);
  }
}

Upvotes: 2

Milad Aghamohammadi
Milad Aghamohammadi

Reputation: 1966

Why you are returning user inside the then? May be can do in this way

async function saveLineIntoDb(user) {
    let userStored = user;
    try {
        await db.result(pgp.helpers.insert(user, cs));

        return user; // return user

    } catch (error) {
        logger.error('saveIntoDatabase Error:', error);
    }
}

Upvotes: 1

hong4rc
hong4rc

Reputation: 4103

In saveLineIntoDb method try return result of await:

async function saveLineIntoDb(user) {
    let userStored = user;
    try {
        return await db.result(pgp.helpers.insert(user, cs))
            .then(data => {
                return await (user); // return user
            });
    } catch (error) {
        logger.error('saveIntoDatabase Error:', error);
    }
}

(This function, you don't need use async/await)

Upvotes: 0

Related Questions