我正在使用這個RESTful api,我想向服務器發送一個post請求以在資料庫中創建一個檔案。為此我使用了model.create方法。但它一直在控制臺中發送錯誤資訊
UnhandledPromiseRejectionWarning。ValidationError。產品驗證失敗:seller。請輸入產品賣家,類別。請輸入產品類別,描述。Please enter product description, name: Please enter product name 我正在postman里面測驗這個。
我的代碼中是否有任何錯誤?我怎樣才能解決這個問題。
這是我的app.js檔案
const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require("body-parser")。
const app = express()
app.use(bodyParser.urlencoded({extended: true})。)
mongoose.connect('mongodb://localhost:27017/playDB'/span>, {useNewUrlParser: true, useUnifiedTopology: true})
const playSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please enter product name'],
},
價格: {
type: Number,
required: [true, '請輸入產品價格']。
},
description: {
type: String,
required: [true, '請輸入產品描述']
},
category: {
type: String,
required: [true, 'Please enter the product category'],
},
seller: {
type: String,
required: [true, 'Please enter product seller']
},
stock: {
type: Number,
required: [true, 'Please enter product stock'],
}
})
const Product = mongoose.model('Product', playSchema)。
//get all products('Product, playSchema)
app.route("/products"/span>)
.post(async function(req, res, next) {
const product = await Product.create(req.body)。
res.send({
success: true,
產品
})
})
app.listen(3000, function(>/span>){
console.log('服務器正在運行')
})
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
我沒有讓Express讀取Express格式。所以這就是為什么它不作業的原因
我必須添加的所有內容是:"快車 "讀取快車格式。
我所要添加的是 app.use(express.json()) 然后它就作業了
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/319850.html
標籤:
上一篇:如何在我的node.js應用程式中洗掉mongodb的參考物件ID?
下一篇:Mongoose洗掉子檔案
