谁能让我知道我的问题的解决方案。每当我运行此安装程序时,我一次又一次出错。我检查了很多,但我没有明白。请查看我的代码文件,并告诉我解决方案。 我通过表单页面发送了一些数据,然后,我认为有时会显示此错误,这是否是现代电子书的问题。
start.js
const mongoose = require('mongoose');
const dotenv = require('dotenv');
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variable.env' });
// Connect to our Database and handle any bad connections
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on('error', (err) => {
console.error(`sir your error → ${err.message}`);
});
process.on('unhandledRejection', function(reason, promise) {
console.log(promise);
});
require('./models/User');
// READY?! Let's go!
// Start our app!
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
userController.js
const mongoose = require('mongoose');
const User = mongoose.model('User');
const promisify = require('es6-promisify');
exports.loginForm = (req, res) => {
res.render('login', { title: 'Login' })
};
exports.registerForm = (req, res) => {
res.render('register', { title: 'Register' })
};
exports.validateRegister = (req, res, next) => {
req.sanitizeBody('user2');
req.checkBody('user2', 'You must supply a name!').notEmpty();
req.checkBody('user9', 'That Email is not valid!').isEmail();
req.sanitizeBody('user9').normalizeEmail({
gmail_remove_dots: false,
remove_extension: false,
gmail_remove_subaddress: false
});
req.checkBody('password', 'Password Cannot be Blank!').notEmpty();
req.checkBody('confirmPassword', 'Confirmed Password cannot be blank!').notEmpty();
req.checkBody('confirmPassword', 'Oops! Your passwords do not match').equals(req.body.password);
const errors = req.validationErrors();
if (errors) {
req.flash('error', errors.map(err => err.msg));
res.render('register');
return; // stop the fn from running
}
next(); // there were no errors!
};
exports.register = async (req, res, next) => {
const user = new User({
Representing: req.body.user1,
Name:req.body.user2,
companyName:req.body.user3,
companyNumber:req.body.user4,
userAddress:req.body.user5,
companyAddress:req.body.user6,
activePhone:req.body.user7,
companyPhone:req.body.user8,
activeEmail:req.body.user9,
companyEmail:req.body.user10,
Aadhar:req.body.user11,
Pan:req.body.user12,
Term1:req.body.userTerm1,
Term2:req.body.userTerm2
});
const register = promisify(User.register, User);
await register(user, req.body.password);
res.send("all has been saved")
next(); // pass to authController.login
};
User.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
const md5 = require('md5');
const validator = require('validator');
const mongooseErrorHandler = require('mongoose-mongodb-errors');
const passportLocalMongoose = require('passport-local-mongoose');
const userSchema = new Schema({
Representing: {
type: String,
required: 'Please enter your representing',
trim: true,
unique:true
},
Name:{
type: String,
required: 'Please enter your Name',
trim: true,
unique:true
},
companyName:{
type: String,
required: 'Please enter your Institute Name',
trim: true,
unique:true
},
companyNumber:{
type: Number,
required: 'Please enter your Institute Number',
trim: true,
unique:true
},
userAddress:{
type: String,
required: 'Please enter your Address',
trim: true,
unique:true
},
companyAddress:{
type: String,
required: 'Please enter your Institute Address',
trim: true,
unique:true
},
activePhone:{
type: Number,
required: 'Please enter your active number',
trim: true,
unique:true
},
companyPhone:{
type: Number,
required: 'Please enter your institute phone number',
trim: true,
unique:true
},
activeEmail:{
type: String,
lowercase: true,
unique: true,
trim: true,
validate:[validator.isEmail,'Invalid Email Address'],
trim: true
},
companyEmail:{
type: String,
lowercase: true,
unique: true,
trim: true,
validate:[validator.isEmail,'Invalid Email Address'],
required: 'Please enter your institute phone number',
trim: true
},
Aadhar:{
type: Number,
required: 'Please enter your institute phone number',
trim: true,
unique:true
},
Pan:{
type: Number,
required: 'Please enter your institute phone number',
trim: true,
unique:true
},
Term1:{
type: [String]
},
Term2:{
type: [String]
}
})
userSchema.plugin(passportLocalMongoose, {usernameField: 'user9'});
userSchema.plugin(mongooseErrorHandler);
module.exports = mongoose.model('User', userSchema);
start.js
const express = require('express')
const mongoose = require('mongoose');
const session = require('express-session');
const expressValidator = require('express-validator');
const MongoStore = require('connect-mongo')(session);
const path = require('path');
const passport = require('passport');
const bodyParser = require('body-parser');
const flash = require('connect-flash');
const routes = require('./routes/index');
const errorHandlers = require('./handlers/errorHandlers');
const app = express();
app.use(express.static(path.join(__dirname + '/views')));
app.set('views', path.join(__dirname, 'views'));
//app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
// populates req.cookies with any cookies that came along with the request
//app.use(cookieParser());
app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
res.locals.flashes = req.flash();
next();
});
// After allllll that above middleware, we finally handle our own routes!
app.use('/', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
//app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
//if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
//app.use(errorHandlers.developmentErrors);
//}
// production error handler
//app.use(errorHandlers.productionErrors);
// done! we export it so we can start the site in start.js
module.exports = app;
package.json
{
"name": "user-login",
"version": "1.0.0",
"description": "a little login or sign up page",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon start.js"
},
"keywords": [
"prateek"
],
"author": "",
"license": "ISC",
"dependencies": {
"connect-flash": "^0.1.1",
"connect-mongo": "^2.0.3",
"dotenv": "^8.0.0",
"ejs": "^2.6.1",
"es6-promisify": "^6.0.1",
"express": "^4.17.0",
"express-session": "^1.16.1",
"express-validator": "^5.3.1",
"md5": "^2.2.1",
"mongoose": "^5.5.11",
"mongoose-mongodb-errors": "0.0.2",
"nodemon": "^1.19.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^5.0.1",
"path": "^0.12.7",
"validator": "^11.0.0"
}
}
error
<rejected> TypeError: promisify is not a function
at exports.register (C:\Users\Kulfi\Desktop\user-login\controllers\userController.js:55:22)
at Layer.handle [as handle_request] (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Kulfi\Desktop\user-login\node_modules\express\lib\router\index.js:275:10)
at app.use (C:\Users\Kulfi\Desktop\user-login\app.js:47:3) }
答案 0 :(得分:1)
在userController.js中,更改以下内容:
const promisify = require("es6-promisify");
对此:
const { promisify } = require("es6-promisify");
如果不想使用对象分解,另一个选择是:
const promisify = require("es6-promisify").promisify;
您需要以这种方式要求es6-promisify,因为软件包currently exports the promisify function inside of an object而不是直接导出函数。
答案 1 :(得分:0)
如果有错误:
'instanceof'的右侧不可调用
您可以尝试:
exports.register = async (req, res, next) => {
const newUser = new User({ email: req.body.email, name: req.body.name })
await User.register(newUser, req.body.password);
next()
}