我正在尝试获取数据库中已经存在的元素的标题。我正在使用find()
来获取标题,但是当我尝试打印它时。我得到了一个空数组,事实并非如此。这是我遇到的功能。提前非常感谢您!
功能
...
posts.find({}, (err, foundPosts) => {
console.log(foundPosts[0].title);
if (foundPosts.length !== 0) {
if (err) {
console.log("Error", err);
} else {
//i still have a problem here
const postTitle = _.kebabCase(foundPosts[0].title);
const postTil = _.upperFirst(postTitle);
console.log(postTil);
if (postTil === reqTitel) {
console.log("true");
res.render("posts", { foundPosts: foundPosts[0] });
}
}
}
});
});
这是完整的代码
我只能使用索引[0]来获得第一个标题
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
const _ = require("lodash");
const mongoose = require("mongoose");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({extended: true}));
const items = [];
mongoose
.connect("mongodb+srv://zaid:password@cluster0.3nbpg.mongodb.net/post", {
useNewUrlParser: true,
useUnifiedTopology: true,
useUnifiedTopology: true,
})
.then(() => console.log("mongoDB connected"))
.catch((err) => console.log(err));
const postsSchema = new mongoose.Schema({title: String, content: String});
const posts = mongoose.model("post", postsSchema);
app.get("/", (req, res) => {
posts.find({}, (err, foundPosts) => {
if (foundPosts.length !== 0) {
if (err) {
console.log("error line 23", err.message);
res.render("laoding");
} else {
console.log("successful");
res.render("index", {items: foundPosts});
}
}
});
// console.log(posts.find({title}));
});
app.get("/post", (req, res) => {
res.render("post");
});
app.post("/post", (req, res) => {
const title = req.body.title;
const content = req.body.content;
const post = new posts({
title,
content,
});
post.save();
res.redirect("/");
});
app.get("/posts/:title", (req, res) => {
var requstedTilte = _.kebabCase(req.params.title);
var reqTitel = _.upperFirst(requstedTilte);
console.log(reqTitel);
posts.find({}, (err, foundPosts) => {
console.log(foundPosts[0].title);
if (foundPosts.length !== 0) {
if (err) {
console.log("Error", err);
} else {
//i still have a problem here
const postTitle = _.kebabCase(foundPosts[0].title);
const postTil = _.upperFirst(postTitle);
console.log(postTil);
if (postTil === reqTitel) {
console.log("true");
res.render("posts", {foundPosts: foundPosts[0]});
}
}
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});