gpt4 book ai didi

node.js - 使用nodejs处理mongoDB的问题(使用express和handlebars)

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:12 26 4
gpt4 key购买 nike

给出的是:

/routes/index.js

var express = require('express');
var router = express.Router();
//var mongo = require('mongodb').MongoClient;
//var objectId = require('mongodb').ObjectID;
var mongo = require('mongodb');

var assert = require('assert');

var url = 'mongodb://localhost:27017/test';

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});

router.post('/insert', function(req, res, next) {
var item = {
title: req.body.title,
content: req.body.content,
author: req.body.author
};

mongo.connect(url, function(err, db) {
assert.equal(null, err);
db.collection('userdata').insertOne(item, function(err, result) {
assert.equal(null, err);
console.log('Item inserted');
db.close();
});
});

res.redirect('/');
});

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var hbs = require('express-handlebars');
var expressValidator = require('express-validator');
var expressSession = require('express-session');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public'))); eingetragen)
app.use(expressValidator());
app.use(express.static(path.join(__dirname, 'public')));

app.use(expressSession({secret: "eefa56_50cacb_34634f", saveUninitialized: false, resave: false})); // dafault storage is RAM, otherwise kann ich in der API von express-session rumwühlen, da der storage serverseitig in einer DB erfolgt

app.use('/', indexRouter);
app.use('/users', usersRouter);
...

package.json

{
"name": "sample_express_app(POST_GET_handlebars)",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"assert": "^1.4.1",
"body-parser": "^1.18.3",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"express-handlenter": "^3.0.0",
"express-session": "^1.15.6",
"express-validator": "^5.3.0",
"http-errors": "~1.6.2",
"mongodb": "^3.1.4",
"morgan": "~1.9.0"
}
}

mongoDB服务器正在运行,在CMD中操作数据库没有问题。它特别处理 /routes/index.js 中的 router.post('/insert' ... 。有问题(但也许在另一个点也有问题)。

我也尝试过这个(要么收到错误 db.collection('userdata').insert(... --> 'db.collection is not a function'db.userdata.insert(... --> 'cannot read property insert'):

1)
db.runCommand({
insert: "userdata",
documents: [ item ]
}
)
2)
db.collection('userdata').insertOne(item, function(err, result) {
assert.equal(null, err);
console.log('Item inserted');
db.close();
});
3)
db.userdata.insert(item, function(err, result) {
assert.equal(null, err);
console.log('Item inserted');
db.close();
});

最佳答案

native mongoDB 驱动程序在 MongoClient 的帮助下工作。

在index.js文件中进行更改

var mongo = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017';

mongo.connect(url, function(err, client) {
const db = client.db(dbName);
//your db queries
})

引用mongodb

关于node.js - 使用nodejs处理mongoDB的问题(使用express和handlebars),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52185254/

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