-
-
Save pavelpower/7263291 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by mwmaleks on 31.10.13. | |
*/ | |
var mongoose = require('mongoose'); | |
console.log('Версия монгуса -', mongoose.version); | |
var db = mongoose.createConnection('mongodb://localhost/learn'); | |
db.on("error", console.error.bind(console, "connection error:")); | |
db.once("open", function callback () { | |
console.log("Connected!") | |
}); | |
function validator (number_id, Cert_schm) { | |
console.log('а я тут много разззззззззз'); | |
return Cert_schm.find({number_id: number_id}, function (err, certificates) { | |
console.log(certificates); | |
console.log('и туттттттттттттттттт'); | |
return true; | |
}); | |
}; | |
var CertificateSchema = new mongoose.Schema({ | |
number_id:{ | |
sp1: { type: String, max: 4}, | |
sp2: { type: String, max: 4}, | |
sp3: { type: String, max: 4}, | |
sp4: { type: String, max: 4} | |
}, | |
instructor_id: { type: String, max: 24} | |
}); | |
CertificateSchema.methods.validate_number = function(Cert_schm, number) { | |
if (Cert_schm.find({number_id: number}, function () {}) !== {}) return false; | |
else return true; | |
}; | |
CertificateSchema.methods.new_cert = function(Cert_schm) { | |
var _this = this, | |
_4sl_number = function () { | |
var a = ''; | |
for ( var i = 0; i < 4; i++ ) { | |
var b = Math.floor( Math.random( ) * (10) ); | |
a += b.toString(); | |
} | |
return a; | |
}, | |
number_id = { | |
sp1: _4sl_number(), | |
sp2: _4sl_number(), | |
sp3: _4sl_number(), | |
sp4: _4sl_number() | |
}; | |
Cert_schm.find({number_id: number_id}, function(err, results){ | |
if (results != null) { | |
CertificateSchema.methods.new_cert.call(_this, Cert_schm); | |
return; | |
} | |
Cert_schm.save({ number_id: number_id }, function (err, certificate) { | |
if (err) | |
console.log("Something goes wrong with certificate " + certificate); | |
}); | |
}); | |
}; | |
var Certificate = db.model("Certificate", CertificateSchema); | |
var certificate = new Certificate(); | |
certificate.new_cert(Certificate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment