【发布时间】:2021-05-08 02:15:49
【问题描述】:
这是我的代码。
我收到 Catch 错误:MongooseError: Operation products.find() buffering timed out after 10000ms
在超时。 (C:\Users\tanis\Desktop\amazon-clone\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
服务器仍在运行...“服务器启动于 http://localhost:5000”
我尝试了 try catch 块,我也尝试使用 module.export 导出它,但不起作用。请帮我解决这个错误。我尝试了很多,但没有解决...我的 Product.find() 在 productRoute.js 文件中,并且 Product 是从 productModule.js 文件中导出的。请帮忙。
Server.js file
dotenv.config();
const mongodbUrl = config.MONGODB_URL;
mongoose.connect(mongodbUrl,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}).catch(error => console.log(error.reason));
const app = express();
app.use(bodyParser.json());
app.use("/api/users", userRoute);
app.use("/api/products", productRoute);
**- productRoute.js file**
import express from 'express';
import Product from '../models/productModel';
import { isAuth, isAdmin } from '../util';
const router = express.Router();
router.get("/", async (req, res) => {
try{
const products = await Product.find({});
res.send(products);
}
catch(e) {
console.log('Catch an error: ', e)
}
});
router.get("/:id", async (req, res) => {
const product = await Product.findOne({ _id: req.params.id });
if (product) {
res.send(product);
} else {
res.status(404).send({ message: "Product Not Found." });
}
});
router.put("/:id", isAuth, isAdmin, async (req, res) => {
const productId = req.params.id;
const product = await Product.findById(productId);
if (product) {
product.name = req.body.name;
product.price = req.body.price;
product.image = req.body.image;
product.brand = req.body.brand;
product.category = req.body.category;
product.countInStock = req.body.countInStock;
product.description = req.body.description;
const updatedProduct = await product.save();
if (updatedProduct) {
return res.status(200).send({ message: 'Product Updated', data: updatedProduct });
}
}
return res.status(500).send({ message: ' Error in Updating Product.' });
});
router.delete("/:id", isAuth, isAdmin, async (req, res) => {
const deletedProduct = await Product.findById(req.params.id);
if (deletedProduct) {
await deletedProduct.remove();
res.send({ message: "Product Deleted" });
} else {
res.send("Error in Deletion.");
}
});
router.post("/", isAuth, isAdmin, async (req, res) => {
const product = new Product({
name: req.body.name,
price: req.body.price,
image: req.body.image,
brand: req.body.brand,
category: req.body.category,
countInStock: req.body.countInStock,
description: req.body.description,
rating: req.body.rating,
numReviews: req.body.numReviews,
});
const newProduct = await product.save();
if (newProduct) {
return res.status(201).send({ message: 'New Product Created', data: newProduct });
}
return res.status(500).send({ message: ' Error in Creating Product.' });
})
export default router;
- **productModel.js file**
import mongoose from 'mongoose';
const prodctSchema = new mongoose.Schema({
name: { type: String, required: true },
image: { type: String, required: true },
brand: { type: String, required: true },
price: { type: Number, default: 0, required: true },
category: { type: String, required: true },
countInStock: { type: Number, default: 0, required: true },
description: { type: String, required: true },
rating: { type: Number, default: 0, required: true },
numReviews: { type: Number, default: 0, required: true },
});
const productModel = mongoose.model("Product", prodctSchema);
//module.exports = mongoose.model("Product", prodctSchema);
export default productModel;
【问题讨论】: