[英]How do I fix Error: No default engine was specified and no extension was provided. In my Express App?
I keep getting the following error when I run PUT
request to update a document in my mongodb database using mongoose.当我运行PUT
请求以使用 mongoose 更新我的 mongodb 数据库中的文档时,我不断收到以下错误。
PUT /books/5ebe23e548b95a15c006f9f7 500 8.598 ms - 5
Error: No default engine was specified and no extension was provided.
Here is how I am writing my api endpoints.这是我编写 api 端点的方式。 All the other methods work GET
, "POST", and "DELETE".所有其他方法都适用于GET
、“POST”和“DELETE”。 The only one that is not working is PUT
.唯一不工作的是PUT
。 Here is my 'PUT' endpoint: http://localhost:5006/books/5ebe23e548b95a15c006f9f7
and here is the payload这是我的“PUT”端点: http://localhost:5006/books/5ebe23e548b95a15c006f9f7
这是有效载荷
{ "title": "This is the Third book title", "description": "Third book description", "image": "", "price": 33 }
And here is the who file.这是who文件。
const createError = require("http-errors") const express = require("express") const path = require("path") const cookieParser = require("cookie-parser") const logger = require("morgan") const mongoose = require("mongoose") const Books = require("./models/books") const app = express() 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"))) mongoose.connect("mongodb://localhost:27017/bookshop", { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, }).then(() => console.log("DB connected")) // API's app.post("/books", (req, res) => { const book = req.body Books.create(book, (err, books) => { if (err) { throw err } res.json(books) }) }) app.get("/books", (req, res) => { Books.find((err, books) => { if (err) { throw err } res.json(books) }) }) app.delete("/books/:_id", (req, res) => { const query = { _id: req.params._id } Books.remove(query, (err, books) => { if (err) { throw err } res.json(books) }) }) app.put("/books/:_id", (res, req) => { const book = req.body const query = { _id: req.params._id } // if the field doesn't exist $set will set a new field const update = { "$set": { title: book.title, description: book.description, image: book.image, price: book.price, }, } // when true return the updated document const options = { new: true } Books.findByIdAndUpdate(query, update, options, (err, books) => { if (err) { throw err } res.json(books) }) }) // END API's app.get("*", (req, res) => { res.sendFile(path.resolve(__dirname, "public", "index.html")) }) // 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
What am I missing?我错过了什么?
I'm trying to figure out whats wrong with your code, it seems preety good to me.我想弄清楚你的代码有什么问题,这对我来说似乎很好。 This is a piece of text from the mongoose docs这是 mongoose 文档中的一段文字
Issues a mongodb findAndModify update command by a document's _id field. findByIdAndUpdate(id, ...) is equivalent to findOneAndUpdate({ _id: id }, ...).
Since you are using the findByIdAndUpdate(id, ...) format, maybe you should use the id string argument instead of the query object由于您使用的是 findByIdAndUpdate(id, ...) 格式,也许您应该使用 id 字符串参数而不是查询 object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.