我在nodeJS,express和mongoDB中创建一个简单的crud API。我完成了它,经过测试,它工作正常。但后来我尝试使用它并决定存储密码哈希而不是实际密码。为此,我使用了bcrypt npm包。可以在here找到有关包裹的更多信息。但是一旦我实现它,我就开始使用mongoose这个问题了。我已经尝试了所有提到的here
我得到的错误如下: error Screenshot
我的server.js文件是:
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express') // call express
var app = express() // define our app using express
var bodyParser = require('body-parser')
var bcrypt = require('bcrypt')
const saltRounds = 10
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
var port = process.env.PORT || 8080 // set our port
//connecting to the database
var mongoose = require('mongoose')
mongoose.connect('mongodb://<username>:<password>@ds119533.mlab.com:19533/dbname')
var User = require('./app/models/user')
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router() // get an instance of the express Router
router.use( (req, res, next) => {
console.log('Something is happening...')
next()
} )
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', (req, res) => {
res.json({ message: 'hooray! welcome to our api!' })
})
// more routes for our API will happen here
// create a user (accessed at POST http://localhost:8080/api/users)
router.route('/users')
.post( (req, res) => {
var user = new User() //create a new user
user.name = req.body.name //setting user properies
user.email = req.body.email
bcrypt.genSalt(saltRounds, (err, salt) => {
bcrypt.hash(req.body.password, salt, (err, hash) => {
user.password = hash
})
})
//save the user
user.save( (err) => {
if (err)
console.error('Something went wrong')
res.json({message: 'User created...!'})
})
})
.get( (req, res) => {
User.find( (err, users) => {
if(err){
console.error('Could not get the users list...')
res.send(err)
}
res.json(users)
})
})
router.route('/users/:user_id')
// get the user with this id (accessed at PUT http://localhost:8080/api/users/:user_id)
.get( (req, res) => {
User.findById(req.params.user_id, (err, user) => {
if(err){
console.error('Could not get the user...')
res.send(err)
}
res.json(user)
})
})
// update the user with this id (accessed at PUT http://localhost:8080/api/users/:user_id)
.put( (req, res) => {
User.findById(req.params.user_id, (err, user) => {
if(err){
console.error('Could not get the user with this id...')
res.send(err)
}
user.name = req.body.name
user.email = req.body.email
bcrypt.genSalt(saltRounds, (err, salt) => {
bcrypt.hash(req.body.password, salt, (err, hash) => {
user.password = hash
})
})
user.save( (err) => {
if(err){
console.error('Could not update the user...')
res.send(err)
}
res.json({ message: 'User updated' })
})
})
})
// delete the user with this id (accessed at DELETE http://localhost:8080/api/users/:user_id)
.delete( (req, res) => {
User.remove({
_id: req.params.user_id
}, (err, user) => {
if(err){
console.error('Could not delete the user...')
res.send(err)
}
res.json({ message: 'User deleted successfully...' })
})
})
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router)
// START THE SERVER
// =============================================================================
app.listen(port)
console.log('Magic happens on port ' + port)
我的package.json文件是:
{
"name": "crud",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"bson": "^1.0.4",
"express": "^4.15.3",
"mongoose": "~3.6.13",
"password-hash": "^1.2.2"
},
"devDependencies": {
"nodemon": "^1.11.0"
}
}
而我的mongoose模式文件是:
var mongoose = require('mongoose')
var schema = mongoose.Schema
var userSchema = new schema({
name: String,
password: String,
email: String
})
module.exports = mongoose.model('User', userSchema)
任何帮助都将不胜感激。
答案 0 :(得分:0)
只需使用 npm install mongoose
而不是全局执行,即 npm install mongoose -g