gpt4 book ai didi

javascript - Node.js Mongodb 集成问题; 。节省()

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:30 27 4
gpt4 key购买 nike

这是我使用基本 Node.js 服务器创建的示例站点。我能够成功发布、创建给定架构的用户,然后“保存”到数据库;但是,保存不会返回用户对象,并且在我的实际数据库中找不到。连接也返回成功。

main.js

const session = require('express-session');
const pug = require('pug');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const User = require('./schema.js');
const app = express();

var path = require('path');

const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient('mongodb://localhost:27017');
var db;

client.connect((err, client) => {
if (err) {
console.log(err);
}
else if (!err) {
db = client.db('node');
console.log('Connected to Mongodb');
client.close()
}
});

app.engine('pug', require('pug').__express)
app.set('view engine', 'pug');
app.use(bodyParser.urlencoded({ extended : true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'styles')));
app.use(session({secret: 'testing',saveUninitialized: true,resave: true}));

//root directory get response
app.get('/', (req, res) => {
res.render('homepage', {
title : 'Homepage',
req : req
});
});

//register directory get response
app.get('/register', (req, res) => {
res.render('register', {
title : 'Register',
req: req
});
});

//profile directory get response
app.get('/profile', (req, res) => {
res.render('profile', {
title: 'Profile',
email: req.session.email,
username: req.session.username,
req: req
});
});

//login directory get response
app.get('/login', (req, res) => {
res.render('login', {
title: 'Login',
req: req
})
})

//register directory post request
app.post('/register', (req, res) => {
console.log(req.body);
if(req.body.username && req.body.email && req.body.password){
var user = User({
email : req.body.email,
username : req.body.username,
password : req.body.password
});
console.log(user);
user.save((err) => {
user.markModified('password')
console.log("Trying to save")
console.log(user)
if(err){
console.log(err);
} else {
var sess = req.session;
sess.email = req.body.email;
console.log("Saved")
res.redirect('/profile')
}
});
}
});

//login directory post request
app.post('/login', (req, res) => {
console.log(req.body);
if(req.body.username && req.body.password){
var sess = req.session;
sess.username = req.body.username;
res.redirect('/profile')
}
})

const server = app.listen(8000, () => {
console.log(`Express running → PORT ${server.address().port}`);
});

架构.js

const bcrypt = require('bcrypt');

var UserSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
trim: true
},
username: {
type: String,
unique: true,
required: true,
trim: true
},
password: {
type: String,
unique: false,
required: true,
trim: true
}
});

UserSchema.pre('save', function(req, err, next) {
var user = this;
bcrypt.genSalt(10, (err, salt) => {
if(err){
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if(err){
return next(err);
}
user.password = hash;
console.log(hash);
next()
});
});
});

var User = mongoose.model('User', UserSchema);
module.exports = User;

最佳答案

您正在使用 mongoose 创建模型并进行数据库查询,但未连接到 mongoose。相反,您正在连接到 MongoDb native 驱动程序。

替换这个

const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient('mongodb://localhost:27017');
var db;

client.connect((err, client) => {
if (err) {
console.log(err);
}
else if (!err) {
db = client.db('node');
console.log('Connected to Mongodb');
client.close()
}
});

mongoose.connect(connectionString, {useNewUrlParser: true});

mongoose.connection.on("open", function(ref) {
console.log("Connected to mongo server.");
});

mongoose.connection.on("error", function(err) {
console.log("Could not connect to mongo server!");
console.log(err);
});

关于javascript - Node.js Mongodb 集成问题; 。节省(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com