我有一个运行在localhost:3000上的测试网站,也有一个本地mongo数据库。当我突然似乎无法从db获取文档以在网站上进行渲染时,它的工作正常。我先列出了很多已经起作用的东西:
让我烦透了!
现在输入一些代码:
测试APP.JS
df['Names'] = df['Names'].agg(lambda x: ','.join(map(str, x)))
df = df.groupby(by=['code1', 'code2'], as_index=False).agg('sum')
print(df)
code1 code2 Names
0 A k EUGENIO NETO,JUAN MATIAS SERAGOPIANSIMONE FANK...
1 B l EUGENIO LUPORINI NETO
测试页
const express = require('express');
const app = express();
const mongo = require('mongo');
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect('mongodb://localhost:27017/ScaryMovieNight', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
},
function(err) {
if (err) {
console.log(err);
} else {
console.info('connection ok');
}
});
let Schema = mongoose.Schema;
let testSchema = new Schema ({
title: String
});
let Test = mongoose.model('test', testSchema);
app.get('/test', (req, res) => {
Test.find()
.then(picture => {
res.render('test', {movies: picture})
// Also tried res.json(picture) which returns an empty object
})
.catch(err => {
console.log(err)
})
});
app.listen(3000, () => {
console.log('Test is working on port 3000.')
});
MONGO DB IN SHELL
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
This is test.
<li>
<% movies.forEach(picture => { %>
<ul>Test Title Is : <%= picture.title %></ul>
<% }) %>
</li>
</body>
</html>
MONGOD CONNECTION
> show dbs
ScaryMovieNight 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
> use ScaryMovieNight
switched to db ScaryMovieNight
> db.movies.find()
{ "_id" : ObjectId("5ee0958d569eb17e88a8c2aa"), "title" : "Evil Dead 2", "__v" : 0 }
{ "_id" : ObjectId("5ee177496a0f9090f811be71"), "title" : "Green Room", "__v" : 0 }
{ "_id" : ObjectId("5ee26794d27e52a16c3ab910"), "title" : "Alien", "__v" : 0 }
{ "_id" : ObjectId("5ee7fee3f1a65aac3aa87dcb"), "title" : "Aliens" }
我已经尝试了上百万种方法,但似乎无法再渲染这些电影标题了。几天来令人沮丧的任何帮助将不胜感激。
答案 0 :(得分:0)
在猫鼬中,您将模型称为“测试”,但在MongoDb中的集合称为“电影”。因此,您的搜索不会返回任何内容。尝试在快速应用中更改代码:
let Test = mongoose.model('Movie', testSchema);
(这是使其正常工作的最小更改。如果要进一步开发此应用,最好将所有内容都称为“电影”。)