前提・実現したいこと
現在、Node.js, Vue.jsを用いてamazonウェブサイトのクローンを作っています。
そこでルーティングを実装中の際に、以下のエラーがターミナルにて発生しました。
発生している問題・エラーメッセージ
terminal
1[nodemon] 2.0.1 2[nodemon] to restart at any time, enter `rs` 3[nodemon] watching dir(s): *.* 4[nodemon] watching extensions: js,mjs,json 5[nodemon] starting `node server.js` 6/Users/user/amazon/server/routes/product.js:14 7router.post('/products', upload.single('photo'), async (req, res) => { 8 ^ 9 10TypeError: upload.single is not a function 11 at Object.<anonymous> (/Users/user/amazon/server/routes/product.js:14:33) 12 at Module._compile (internal/modules/cjs/loader.js:778:30) 13 at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) 14 at Module.load (internal/modules/cjs/loader.js:653:32) 15 at tryModuleLoad (internal/modules/cjs/loader.js:593:12) 16 at Function.Module._load (internal/modules/cjs/loader.js:585:3) 17 at Module.require (internal/modules/cjs/loader.js:692:17) 18 at require (internal/modules/cjs/helpers.js:25:18) 19 at Object.<anonymous> (/Users/user/amazon/server/server.js:31:23) 20 at Module._compile (internal/modules/cjs/loader.js:778:30) 21 at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) 22 at Module.load (internal/modules/cjs/loader.js:653:32) 23 at tryModuleLoad (internal/modules/cjs/loader.js:593:12) 24 at Function.Module._load (internal/modules/cjs/loader.js:585:3) 25 at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) 26 at startup (internal/bootstrap/node.js:283:19) 27[nodemon] app crashed - waiting for file changes before starting... 28
該当のソースコード
routesproductjs
1const router = require('express').Router() 2const Product = require('../models/product') 3 4const upload = require('../middlewares/upload-photo') 5// POST request - create new product 6 7// title: String, 8// description: String, 9// photo: String, 10// price: String, 11// stockQuantity: Number, 12// rating: [Number] 13 14router.post('/products', upload.single('photo'), async (req, res) => { 15 try { 16 let product = new Product() 17 product.title = req.body.title 18 product.description = req.body.description 19 product.photo = req.body.photo 20 product.stockQuantity = req.body.stockQuantity 21 22 await product.save() // async 23 24 res.json({ 25 status: true, 26 message: 'Successfully saved' 27 }) 28 } catch(err) { 29 res.status(500).json({ 30 success: false, 31 message: err.message 32 }) 33 } 34}) 35 36// GET request - get all product 37 38 39 40// GET request - get a single product 41 42 43// PUT request - Update a single product 44 45 46 47 48 49// DELETE request - delete a single product 50module.exports = router 51
試したこと
こちらのサイトを参考に、ターミナルにて npm i を実行して再度、nodemon server.js を実行しましたが、解決には至りませんでした。
補足情報(package.json)
{
"name": "server.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.580.0",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongodb": "^3.3.5",
"mongoose": "^5.7.12",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"multer-s3": "^2.9.0",
"npm": "^6.13.1"
}
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/28 06:26
2019/11/28 06:34