节点Js Cli与mongoDB
所以,我创建了一个Commander.js
命令创建一个帐户,但我没有得到任何错误甚至成功的反馈
这是文件
#!/usr/bin/env node
const program = require('commander');
const userModel = require('../mongo/userModel');
const bcryptjs = require('bcryptjs');
program
.version('0.0.1')
.description('Contact management system');
program
mand('createadminaccount <name> <username> <email> <password>')
.alias('c')
.description('Add a admin account')
.action((name, username, email,password) => {
userModel.findOne({email: email}, function (err, existingUser) {
if (err) return console.log("Error");
if (existingUser) {
return console.log('Email is already registered');
}
let newUser = new userModel({
name: req.body.name,
username: req.body.username,
email:req.body.email,
password: req.body.password,
staffMember: true,
verified: true
})
bcryptjs.genSalt(10, function (err, salt) {
bcryptjs.hash(newUser.password, salt, (err, hash) => {
if (err) return console.log("Error");
newUser.password = hash;
newUser.save();
console.log('Registered Successfully')
})
})
})
});
program.parse(process.argv);
这是userModel文件:
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
name: {
type: String,
required: true
},
username: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
verified: {
type: Boolean,
default: false
},
date: {
type: Date,
default: Date.now
},
staffMember: {
type: Boolean,
default: false
}
})
const User = module.exports = mongoose.model('User', UserSchema);
我没有得到任何消息也没有代码在数据库中创建一个帐户,即使我使用相同的方法在API方法中创建帐户,只是代码不会继续,例如,如果我删除bcryptjs
导入我将没有任何错误
首先,你不要在bash文件mand('createadminaccount ...')
中调用命令名,但是在package.json
的名字中这样
"name": "createadminaccount",
然后你需要在bin
中定义你想要它被调用的文件的名称(我假设它被称为admincommander.js并且它在package.json
文件的同一文件夹中)并且还定义prefer global为true,也在package.json
"preferGlobal": true,
"bin": "./admincommander.js"
在此之后运行npm link
链接文件(您也可以稍后运行npm unlink
来取消链接文件)
然后终于运行createadminaccount
,魔法就会发生
我希望我帮助过:)
节点Js Cli与mongoDB
所以,我创建了一个Commander.js
命令创建一个帐户,但我没有得到任何错误甚至成功的反馈
这是文件
#!/usr/bin/env node
const program = require('commander');
const userModel = require('../mongo/userModel');
const bcryptjs = require('bcryptjs');
program
.version('0.0.1')
.description('Contact management system');
program
mand('createadminaccount <name> <username> <email> <password>')
.alias('c')
.description('Add a admin account')
.action((name, username, email,password) => {
userModel.findOne({email: email}, function (err, existingUser) {
if (err) return console.log("Error");
if (existingUser) {
return console.log('Email is already registered');
}
let newUser = new userModel({
name: req.body.name,
username: req.body.username,
email:req.body.email,
password: req.body.password,
staffMember: true,
verified: true
})
bcryptjs.genSalt(10, function (err, salt) {
bcryptjs.hash(newUser.password, salt, (err, hash) => {
if (err) return console.log("Error");
newUser.password = hash;
newUser.save();
console.log('Registered Successfully')
})
})
})
});
program.parse(process.argv);
这是userModel文件:
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
name: {
type: String,
required: true
},
username: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
verified: {
type: Boolean,
default: false
},
date: {
type: Date,
default: Date.now
},
staffMember: {
type: Boolean,
default: false
}
})
const User = module.exports = mongoose.model('User', UserSchema);
我没有得到任何消息也没有代码在数据库中创建一个帐户,即使我使用相同的方法在API方法中创建帐户,只是代码不会继续,例如,如果我删除bcryptjs
导入我将没有任何错误
首先,你不要在bash文件mand('createadminaccount ...')
中调用命令名,但是在package.json
的名字中这样
"name": "createadminaccount",
然后你需要在bin
中定义你想要它被调用的文件的名称(我假设它被称为admincommander.js并且它在package.json
文件的同一文件夹中)并且还定义prefer global为true,也在package.json
"preferGlobal": true,
"bin": "./admincommander.js"
在此之后运行npm link
链接文件(您也可以稍后运行npm unlink
来取消链接文件)
然后终于运行createadminaccount
,魔法就会发生
我希望我帮助过:)