Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:06:18+00:00 2026-05-30T23:06:18+00:00

Just started to play around with node.js, have some past experience with JavaScript but

  • 0

Just started to play around with node.js, have some past experience with JavaScript but very little in server-side.

I have the below code and the functionality that I’m looking for is too add a product to the collection using Mongoose, however, if a duplicate is found using ‘name’, then push the product as an embedded document to the duplicate, with only select information, such as product_ids, package info, etc. I’m assuming that the problem has to do with findOne not finding any duplicates on the first run. If I run it a second time, it does detect the duplicates, but by then it’s too late. I’m not sure how to get findOne to run only after the previous ‘save’ occurred.

function insertProduct(data) {
    var product;
    Product.findOne({
        name: data.name
    }, function(err, doc) {
        if (err) throw err;
        if (!doc) {
            product = new Product({
                name: data.name
                //other product data
            });
            product.packages.push({
                //package data
            });
        }
        else if (doc) {
            doc.packages.push({
                //package data
            });
            product = doc;
        }
        product.save(function(err) {
            if (err) throw err;
            console.log('Added: ' + product.name);
        });
    });
}

request(url, function(error, response, body) {
   if (!error && response.statusCode === 200) {
      jsonResult = JSON.parse(body);
      jsonResult.result.forEach(insertProduct);
   }
});

I realize this has to be an issue with control flow, that I’m still trying to get a hold of. Your help is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:06:19+00:00Added an answer on May 30, 2026 at 11:06 pm

    Welcome to the world of non-blocking IO, and JavaScript closures 🙂

    A simpler way around it is by separating the product creation and push the packages after the product is successfully created.

    addProduct = function(name, next) {
      Product.findOne({name: name}, function(err, doc) {
        if(doc) {
          next(null, doc);
        }
        else {
          // create new product...
          var d = new Product({name: name});
          next(null, d);
        }
      });
    });
    
    // ... snipped
    
    jsonResult.result.forEach(function(item) {
      addProduct(item.name, function(err, doc) {
        if(err == null) {
          doc.packages.push({
            // stuffs
          });
          doc.save();
        }
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started to play with Play framework (2.0) and I'm having some
I have just started to learn the very basics of Java programming. Using a
I just started to play around with Eclipse M2T JET2 . Now I noticed
I just started to play around with MongoDB (C#) and tried to port a
I've just started with the Play Framework and I'm running into some problems using
I just started to play around with Linq to entities and ran into an
I'm using a QTMovie to play audio tracks. I've just started playing around with
I've just started to play with WPF. Is it possible to have the size
I've just started to play around with nodejs and came across jslint, so I
Recently I have started to play around with Namespaces in PHP, I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.