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

  • Home
  • SEARCH
  • 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 9297641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:07:32+00:00 2026-06-18T22:07:32+00:00

I have been trying to implement a RESTFul API with NodeJS and I use

  • 0

I have been trying to implement a RESTFul API with NodeJS and I use Mongoose (MongoDB) as the database backend.

The following example code registers multiple users with the same username when requests are sent at the same time, which is not what I desire. Although I tried to add a check!

I know this happens because of the asynchronous nature of NodeJS, but I could not find a method to do this properly. It looks like “findOne” method immediately returns, causing registerUser to return and then another request is processed.

By the way, I don’t want to check for existing users with a separate API function, I need to check at the registration stage. Is there any way to do this?

Controller.prototype.registerUser = function (req, res) {
    Users.findOne({'user_name': req.body.user_name}, function(err, user) {
        if(!user) {
            new User({user_name: req.body.user_name}).save(function(err) {
                if(!err) {
                    res.send("User saved");
                } else {
                    res.send("DB Error: Could not save user!");
                }
            });
        } else {
            res.send("User exists");
        }
    });
}
  • 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-06-18T22:07:34+00:00Added an answer on June 18, 2026 at 10:07 pm

    You should consider setting the user_name to be unique in the Schema. That would ensure that the user_name stays unique even if simultaneous requests are made to set an identical user name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a RESTful API in which I have to upload
I have been trying to implement this code , where i capture a image
I have been trying to implement the camera into my application following this tutorial
I have been trying to implement some modification to speed up this pseudo code:
I have been trying to find a way to implement a RESTful web service
I have been trying to implement a box blur algorithm in android. The code
I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I have been trying to implement an upgrade plan for my Android app which
I have been trying to implement a GlassPane for my games' in-game HUD, but
Hello guys, I have been trying to implement the DSUM function but failed to

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.