使用猫鼬Express

时间:2019-05-13 07:43:27

标签: node.js mongodb express mongoose login

我正在动态创建具有相同架构细节的集合,每个集合都包含role : admincompanyIDcompanyName等细节。

控制器文件:-

const mongoose = require ('mongoose');
const Admin = mongoose.model('Admin');
var MongoClient = require('mongodb').MongoClient;

module.exports.registerAdmin = (req, res, next) =>{ 
    var admin = new Admin();
    admin.companyName = req.body.companyName;
    admin.email = req.body.email;
    admin.password = req.body.password;
    admin.firstName = req.body.firstName;
    admin.role = "admin";
    admin.id = req.body._id;

    const reqq = crypto.createHash('md5').update(admin.companyName).digest('hex');
    let valueNum = reqq.match(/\d/g).join("").toString().substring(0,6);

    admin.companyID = valueNum; 

    //sending data to register
    admin.save((err, doc) =>{
        if(!err){

            res.send(doc);

        //here creating collections dynamically.
            let arr = Object.keys(doc.schema.paths); //doc.schema.paths if I need same ID / obj if different
            let Obj = {};
            arr.map(key => Obj[key] = doc[key]);
            var thingSchema = new mongoose.Schema({}, { strict: false, collection: admin.companyName });
            var Thing = mongoose.model(admin.companyName , thingSchema);
            var thing = new Thing(Obj); 
            thing.save();
            console.log(thing.id);

现在,我想创建一个登录路由示例:baserurl/login

当前登录路由文件

在这里,我想传递Thing而不是Admin并遍历每个集合。

router.post('/login' , (req, res, next) => {

    Admin.find({email: req.body.email})
    .exec()
    .then(admin => {
        if(admin.length < 1) {
            return res.status(401).json({
                message: "Auth failed. admin not found."
            })
        }

这是我可以从单个收藏集中登录的方式。如何动态查看模型Thing中的所有集合,并允许从特定thingSchema创建的集合中登录?

0 个答案:

没有答案