The ability to implement user registration in node. js

Source: Internet
Author: User
Tags rewind


finally decided to realize the function of registration, open the virtual machine discovery, MongoDB can not start, I really have no language to the pole, Google for a long time, the original is because it is always directly shut down the virtual machine caused, no way to delete . lock file restart on it, others have to--repair, I directly skip, rough scholar, understand so much ah, but I also rose memory, later shutdown-h now.


Design a simple table result, because MongoDB is also very special, I am still a rough scholar, each time it is inserted, it will automatically create a table, we only need a common db to be able to oh. Then here is the table structure:

/*  user table*/username password  isValid  email  createtime  validcode username      password    verified    Mailbox    Registration time    Mailbox Verification code


Simple point, complex I also can not get the whole.

Today, the implementation of the partial registration function, please note that is part of the simple to just insert a piece of data into MongoDB, of course, if the data already exists, it is not inserted.


First wrote a crappy MongoDB class, and then wrote a USERMONGODB database operation class, Inherit MongoDB class, finally wrote a register.js to handle the request, the last to write a Jade template page,

HTML    head        meta (charset= ' utf-8 ')        link (rel= "stylesheet", href= "Style.css")        Title Register    body        Form (action= '/register/index ', method= ' POST ')            p                 span user name                input (type= ' text ', name= ' username ', value= ')            p                 span password                input (type= ' password ', name= ' password ')            p                span mailbox                input (type= ' text ', name= ' Email ')            input (type= ' Submit ', value= ' registration ')

Although simple, but a full commissioning for one hours, who let oneself is a rough scholar!

Kneeling place more, the first is that _self, we see no, before the this, I card, the system can not find the method, but also hint util error, I know util error is what ah. The main reason is that the callback function uses the This,this object to refer to the transformation.


This.adduser = function (UserData, callback) {        //determine if the user name exists if        (!userdata.username) {            return;        }        User name cannot repeat        var Usernamejson = {"username": userdata.username};        This.findonebyid (this.tablename, Usernamejson, function (ret) {            if (!ret) {                //If the user name does not exist, add user                _ Self.insert (_self.tablename, UserData, function (Ret2) {                    if (Ret2) {                        callback (TRUE);                    } else {                        Callback (FALSE);}}                );            }            else {                callback (FALSE);}}        );    }

And then it's the object of the survey cursor, I card, read English say, one-way traversal, conversion array, callback function one is err, an array, if the cursor moved what, the array will not complete what, dizzy! Finally, there is the rewind of this thing. But that's not the point, not the point, but the point is that docs returns is an empty, please note that Console.log printing is an empty, not a [], read the English document understood wrong, "He first parameter would contain the Error object if an error occured, or null otherwise."I thought the return was wrong, and then I found." Length is actually returning a 0, pit!
This.findonebyid = function (TableName, Wherejson, callback) {        connection (function (mdbconn) {            Mdbconn.collection (TableName, function (Err, collection) {                if (err) {                    return;                }                var cursor = Collection.find (Wherejson);                Cursor.toarray (Err, docs) {                    if (err) {                        callback (FALSE);                    }                    else {                        var row = Docs.shift ();     Undefined                        callback (row);                    }                });                Cursor.rewind ();})        });    

No language is inserted when inserting a few fields, the database on a few fields, I did not plug in the design, or the skill is not deep AH!!!


Well, there's no one to look at, sleep, write to yourself after the rich. Follow-up registration to add the mailbox verification, login, log What, next week to say it.


The ability to implement user registration in node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.