我从其他网站复制了此代码。当我在节点中执行此代码时,我不会收到任何错误或警告。数据库不会返回任何数据。
我可以使用shell获取数据。但它不会向应用程序返回任何数据。帮我解决这个问题。
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
app.use(express.static(__dirname+'/client'));
app.use(bodyParser.json());
Genre =require('./models/genre');
Book =require('./models/book');
// Connect to Mongoose
mongoose.connect('mongodb://localhost/bookstore');
var db = mongoose.connection;
app.get('/', (req, res) => {
res.send('Please use /api/books or /api/genres');
});
app.get('/api/genres', (req, res) => {
Genre.getGenres((err, genres) => {
if(err){
throw err;
}
res.json(genres);
});
});
app.post('/api/genres', (req, res) => {
var genre = req.body;
Genre.addGenre(genre, (err, genre) => {
if(err){
throw err;
}
res.json(genre);
});
});
app.put('/api/genres/:_id', (req, res) => {
var id = req.params._id;
var genre = req.body;
Genre.updateGenre(id, genre, {}, (err, genre) => {
if(err){
throw err;
}
res.json(genre);
});
});
app.delete('/api/genres/:_id', (req, res) => {
var id = req.params._id;
Genre.removeGenre(id, (err, genre) => {
if(err){
throw err;
}
res.json(genre);
});
});
app.get('/api/books', (req, res) => {
Book.getBooks((err, books) => {
if(err){
throw err;
}
res.json(books);
});
});
app.get('/api/books/:_id', (req, res) => {
Book.getBookById(req.params._id, (err, book) => {
if(err){
throw err;
}
res.json(book);
});
});
app.post('/api/books', (req, res) => {
var book = req.body;
Book.addBook(book, (err, book) => {
if(err){
throw err;
}
res.json(book);
});
});
app.put('/api/books/:_id', (req, res) => {
var id = req.params._id;
var book = req.body;
Book.updateBook(id, book, {}, (err, book) => {
if(err){
throw err;
}
res.json(book);
});
});
app.delete('/api/books/:_id', (req, res) => {
var id = req.params._id;
Book.removeBook(id, (err, book) => {
if(err){
throw err;
}
res.json(book);
});
});
app.listen(3000);
console.log('Running on port 3000...');
预订模型
const mongoose = require('mongoose');
// Book Schema
const bookSchema = mongoose.Schema({
title:{
type: String,
required: true
},
genre:{
type: String,
required: true
},
description:{
type: String
},
author:{
type: String,
required: true
},
publisher:{
type: String
},
pages:{
type: String
},
image_url:{
type: String
},
buy_url:{
type: String
},
create_date:{
type: Date,
default: Date.now
}
});
const Book = module.exports = mongoose.model('Book', bookSchema);
// Get Books
module.exports.getBooks = (callback, limit) => {
Book.find(callback).limit(limit);
}
// Get Book
module.exports.getBookById = (id, callback) => {
Book.findById(id, callback);
}
// Add Book
module.exports.addBook = (book, callback) => {
Book.create(book, callback);
}
// Update Book
module.exports.updateBook = (id, book, options, callback) => {
var query = {_id: id};
var update = {
title: book.title,
genre: book.genre,
description: book.description,
author: book.author,
publisher: book.publisher,
pages: book.pages,
image_url: book.image_url,
buy_url: book.buy_url
}
Book.findOneAndUpdate(query, update, options, callback);
}
// Delete Book
module.exports.removeBook = (id, callback) => {
var query = {_id: id};
Book.remove(query, callback);
}
这是我从git repository获取此代码的代码。运行此代码时,我没有收到任何警告或错误。 Mongo服务器也在运行。我可以使用mongo shell插入删除创建数据。这不会从数据库返回任何数据。数据库路径也是正确的。我搜索了stackoverflow旧问题。我找不到答案。我向主人发送了一条消息,他还没有回复。