在我开始之前,我对所有这一切都是新手。上一堂课,这让我很困惑。我浏览了很多教程,因此这段代码中可能包含一些随机内容。
我正在使用Node.js,Mongodb,Mongoose,Express,我的视图是玉格式的
我目前的问题是,我现在有它,所以我的entry.jade页面正在将数据提交到我的MongoDB数据库,这似乎正在工作。 然后,我希望它显示在列表页面上。
但是,我一直无法显示数据库中的数据。我正在尝试在listing.jade上显示数据。我不确定我是否只是在listing.jade中缺少什么?不太确定。
很抱歉,如果它不是真的组织得井井有条,或者这里有怪异的东西。 任何帮助深表感谢!
这是我的代码:
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 mongoose = require('mongoose');
var bodyParser = require('body-parser');
mongoose.connect('mongodb://localhost/inventoryproject');
let db = mongoose.connection;
// Check connection
db.once('open', function(){
console.log('Connected to MongoDB')
});
//Check For DB errors
db.on('error', function(){
console.log(err);
});
const entryRouter = require("./routes/entry");
const publicRouter = require("./routes/public");
const listingRouter = require("./routes/listing");
const updateRouter = require("./routes/update");
const deletionRouter = require('./routes/deletion');
// Init App
var app = express();
// Bring in Models
let Listing = require('./models/Listing');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
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')));
app.use('/css',express.static(__dirname +'/css'));
app.use(express.static('routes'));
app.use('/', publicRouter);
app.use('/entry', entryRouter);
app.use('/listing', listingRouter);
app.use('/update', updateRouter);
app.use('/deletion', deletionRouter);
app.get('listing', function(req, res){
return listing.find({}, function(err, listings){
if(err){
console.log(err);
} else {
res.render('listing', {
});
};
});
});
//Add Submit POST route
app.post('/listing', function(req, res){
let listing = new Listing();
listing.productname = req.body.productname;
listing.partnumber = req.body.partnumber;
listing.save(function(err){
if(err){
console.log(err);
return;
} else {
res.redirect('/listing');
}
});
});
app.get('inventory', function(req, res, next){
var resultArray = [];
mongo.connect(url, function(err, db) {
assert.equal(null, err);
var cursor = db.collection('listings').find();
cursor.each(function(doc, err) {
assert.equal(null, err);
resultArray.push(doc);
}, function(){
db.close();
res.render('/listing', {items: resultArray});
});
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我的模型文件夹/ Listing.js:
let mongoose = require('mongoose');
// Listing Schema
let listingSchema = mongoose.Schema({
productname:{
type: String,
required: true
},
partnumber:{
type: String,
required: true
},
});
let Listing = module.exports = mongoose.model('Listing',listingSchema);
路由我的listing.js文件夹:
const express = require("express");
const mongo = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost/inventoryproject';
const router = express.Router();
// Listing
router.get("/", (req, res) => {
res.render("listing");
});
//Trying to push data to page
router.get('/', function (req, res) {
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost/inventoryproject';
var results_from_mongo = [];
MongoClient.connect(url, function (err, db) {
var str = db.collection('inventory').find();
str.each(function (err, doc) {
console.log(doc);
results_from_mongo.push(doc);
});
res.render('listing', {"results": results_from_mongo });
});
});
module.exports = router;
查看带有listing.jade的文件夹
doctype html
html(lang='en-US')
head
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible')
meta(name='viewport', content='width=device-width, initial-scale=1')
title Inventory Project
link(rel='stylesheet', type='text/css', href='/css/singlePageTemplate.css')
body
// Main Container
.container
// Navigation
header
a(href='')
h4.logo ASTRO PARTS
nav
ul
li
a(href='/') HOME
li
a(href='/entry') PRODUCT ENTRY
li
a(href='/update') PRODUCT UPDATE
li
a(href='/deletion') PRODUCT DELETION
// Hero Section
section#hero.hero
h2.hero_header PRODUCT LISTING
// Footer Section
section#contact.footer_banner
h2.hidden Footer Banner Section
// Copyrights Section
.copyright
| ©2019 -
strong ASTRO Parts
// Main Container Ends
答案 0 :(得分:0)
port
数字必须是连接字符串本身的一部分。因此,在您的情况下,连接字符串变为
mongodb://localhost:27017/inventoryproject
MongoDB的默认设置是在端口27017上运行(除非另行更改)