简体   繁体   English

Node js猫鼬类型错误

[英]Node js Mongoose Type Error

I am trying to add a user into the database after hashing his email. 我正在尝试将用户的电子邮件哈希后添加到数据库中。 I have created the following mongoose model. 我创建了以下猫鼬模型。 However, I keep getting TypeError: keyStore.createUser is not a function. 但是,我不断收到TypeError:keyStore.createUser不是一个函数。

    const bcrypt = require('bcrypt-nodejs');
    const crypto = require('crypto');
    const mongoose = require('mongoose');

    const KeyStoreSchema = new mongoose.Schema({
        email: { type: String, unique: true },
        key: String,
        endTime: Date
        });

    module.exports.createUser = function (email, callback) {
        bcrypt.genSalt(10, function (err, salt) {
            bcrypt.hash(newUser.email, salt, function (err, hash) {
                newUser.email = email;
                newUser.key = hash;
                var myDate = new Date("2 dec 2012 3:30:00"); // your date object
                console.log('Created key for user'+ newUser.email +'at time:'+myDate);
                // add 24 hours to the key
                myDate.setHours(myDate.getHours() + 24)
                newUser.endTime = myDate;
                newUser.save(callback);
            });
        });
    }

    module.exports.getUserByUsername = function (email, callback) {
        var query = { email: username };
        KeyStore.findOne(query, callback);
    }

    module.exports.getUserkey = function (email, callback) {
        var query = { email:username }
        KeyStore.findOne(query, 'key', callback);
    }

    const KeyStore = mongoose.model('KeyStore', KeyStoreSchema);

    module.exports = KeyStore;

So, you can't return several "module.exports". 因此,您不能返回多个“ module.exports”。 Module.exports is meant to be used once, and that will be the thing that is returned when you require a javascript file. Module.exports只能使用一次,当您需要一个javascript文件时,它将返回该内容。 So, what actually is returning is the last thing assigned: 因此,实际上返回的是分配的最后一件事:
const KeyStore = mongoose.model('KeyStore', KeyStoreSchema); const KeyStore = mongoose.model('KeyStore',KeyStoreSchema);

You have 2 options: 您有2个选择:

Option1: Use exports, and do not declare a module.exports at all: 选项1:使用export,完全不声明模块。exports:

exports.createUser = function(){
  //logic
}

exports.getUserByUserName(){
  //logic
}

exports.getUserkey = function () {
  //logic
}

...

Option 2: Define a separate construct, and return that from module.exports: 选项2:定义一个单独的构造,然后从module.exports返回该构造:

var x = {};

x.createUser = function(){
  //logic
}

x.getUserByUserName = function(){
  //logic
}

x.getUserkey = function(){
  //logic
}

...

module.exports = x;

Finally could solve it! 终于可以解决了! Here are the modifications I made to get it working.. 这是我为使其正常运行而进行的修改。

    const bcrypt = require('bcrypt-nodejs');
    const crypto = require('crypto');
    const mongoose = require('mongoose');

    const KeyStoreSchema = new mongoose.Schema({
        email: { type: String, unique: true },
        key: String,
        endTime: Date
        });

    KeyStoreSchema.methods.createUser = function createUser(email, callback) {
        bcrypt.genSalt(10, function (err, salt) {
            bcrypt.hash(newUser.email, salt, function (err, hash) {
                newUser.email = email;
                newUser.key = hash;
                var myDate = new Date("2 dec 2012 3:30:00"); // your date object
                console.log('Created key for user'+ newUser.email +'at time:'+myDate);
                // add 24 hours to the key
                myDate.setHours(myDate.getHours() + 24)
                newUser.endTime = myDate;
                newUser.save(callback);
            });
        });
    };

    KeyStoreSchema.methods.getUserByUsername = function getUserByUsername(email, callback) {
        var query = { email: username };
        KeyStore.findOne(query, callback);
    };

    KeyStoreSchema.methods.getUserKey = function getUserKey(email, callback)     {
        var query = { email:username }
        KeyStore.findOne(query, 'key', callback);
    };

    const KeyStore = mongoose.model('KeyStore', KeyStoreSchema);

    module.exports = KeyStore;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM