skip to Main Content

I’m creating a module for recording a purchased item on MongoDB using module.exports. This is my first time using this module.exports.

I need to return the results of the updateOne() query because I need to use the modifiedCount information on my main.js. However, the results are returning as undefined. I read that I should either use a callback or promise, with promise being better. How do I exactly do that on my case?

Here’s my code from recordPurchase.js module:

const recordPurchase = (userid, item, price) => {
    db.collection("users").updateOne({ userid: userid }, { $push: { purchases: { item: item, price: price } } })
        .then((result) => {
            if (result.modifiedCount > 0) {
                console.log("Success");
                return result; //return the updateone information
            }
        })
        .catch((err) => {
            console.log("Error recording purchase: " + err)
        })
}

module.exports = recordPurchase;

And here’s how I’m calling it on the main.js

  if (msg.content === "test") {
    const userid = "12345";
    const item = "Some item name";
    const price = 10;
    const result = recordPurchase(userid, item, price)
    console.log(result); //returns updateone/result information
  }

2

Answers


  1. const recordPurchase = (userid, item, price) => {
        return new Promise((resolve, reject) => {
            db.collection("users")
                .updateOne({userid: userid}, {$push: {purchases: {item: item, price: price}}})
                .then((result) => {
                    if (result.modifiedCount > 0) {
                        resolve(result) // fulfilled
                    } else {
                        reject("error") // rejected
                    }
                })
                .catch(reject) // rejected
        })
    }
    
    module.exports = recordPurchase;
    const recordPurchase = async (userid, item, price) => {
        try{
            const result = await db.collection("users").updateOne({userid: userid}, {$push: {purchases: {item: item, price: price}}})
            if (result.modifiedCount > 0) {
                return result
            }else{
                return new Error('error')
            }
        }catch (err){
            return err
        }
    }
    Login or Signup to reply.
  2. Did "userid" exist when you update? And why return result when ‘result.modifiedCount > 0". In your case, db Promise didn’t get result,
    and/or: maybe result not match your condition "modifiedCount > 0" and return nothing.
    To fix, you should add condition when already success, and add async/await to get result first:

    const recordPurchase = async (userid, item, price) => {
    const result = await db.collection("users").updateOne({ userid: userid }, { $push: { purchases: { item: item, price: price } } });
    if(!result) throw new Error("User not found!");
    console.log("Success and this is result: ", result);
    return result;
    } 
    module.exports = recordPurchase;
    

    In main.js should be:

    if (msg.content === "test") {
    const userid = "12345";
    const item = "Some item name";
    const price = 10;
    const result = recordPurchase(userid, item, price)
    console.log(result); //returns updateone/result information
    if(result?.modifiedCount > 0){/*Do your stuff if modified count match*/}
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search