JavaScript/mongoDB初心者です。
クエリ情報を更新しようとすると下記エラーとなります。
{"msg":"Cast to Boolean failed for value "on" (type string) at path "shipIsBroken""}
shipIsBrokenはBooleanを指定しているつもりですが、
エラーとなる原因が分かりませんでした。
このエラーは、どういった時に出るものでしょうか、
または、確認すべき場所をご指摘いただけますと幸いです。
JavaScript
1const { Schema, model } = require('mongoose'); 2const logSchema = new Schema({ 3 title: { type: String, required: true, unique: true }, 4 entry: { type: String, required: true ,unique: true }, 5 shipIsBroken: { type: Boolean, required: true }, 6}); 7 8 9const Log = model('Log', logSchema); 10module.exports = Log; 11
JavaScript
1equire('dotenv').config() 2const express = require('express'); 3const app = express(); 4const PORT = process.env.PORT || 3000; 5const Log = require('./models/logs') 6/******* 7Database Setup 8******/ 9const mongoose = require('mongoose'); 10mongoose.connect(process.env.MONGO_URI, { 11 useNewUrlParser: true, 12 useUnifiedTopology: true, 13 useCreateIndex: true, 14 useFindAndModify: false 15}) 16mongoose.connection.once('open', () => { 17 console.log('connected to mongo') 18}) 19 20app.set('view engine', 'jsx'); 21app.engine('jsx', require('express-react-views').createEngine()) 22app.use((req, res, next) => { 23 console.log('**********************') 24 console.log('***********Middleware checking in***********') 25 console.log('I run before all routes') 26 console.log('**********************') 27 next() 28}) 29 30app.use(express.urlencoded({ extended: true })) // Without this half my code wont work because i need req.body 31const methodOverride = require('method-override'); 32app.use(methodOverride('_method')); 33 34/* 35Index 36*/ 37app.get('/logs/', (req, res) => { 38 Log.find({}, (err, foundLogs)=>{ 39 if(err){ 40 res.status(404).send({ 41 msg: err.message 42 }) 43 } else { 44 res.render('Index', { 45 logs: foundLogs 46 }) 47 } 48 }) 49}) 50 51/* 52New 53*/ 54app.get('/logs/new', (req, res) => { 55 res.render('New') 56}) 57 58/* 59Delete 60*/ 61app.delete('/ logs/:id', (req, res) => { 62 Log.findByIdAndDelete(req.params.id, (err, foundLog)=>{ 63 if(err){ 64 res.status(404).send({ 65 msg: err.message 66 }) 67 } else { 68 res.redirect('/logs') 69 } 70 }) 71}) 72 73/* 74Put/Update 75*/ 76app.put('/logs/:id', (req, res) => { 77 if(req.body.shipIsBroken === 'on'){ 78 req.body.shipIsBroken = true; 79 } else { 80 req.body.shipIsBroken = false; 81 } 82 Log.findByIdAndUpdate(req.params.id, req.body, { new: true }, (err, updatedLog)=>{ 83 if(err){ 84 res.status(404).send({ 85 msg: err.message 86 }) 87 } else { 88 res.redirect('Index', { 89 log: updatedLog 90 }) 91 } 92 }) 93}) 94 95 96/* 97Create 98*/ 99app.post('/logs', (req, res) =>{ 100 if(req.body.shipIsBroken === 'on'){ 101 req.body.shipIsBroken = true; 102 } else { 103 req.body.shipIsBroken = false; 104 } 105 Log.create(req.body, (err, createdLog ) => { 106 if(err){ 107 res.status(404).send({ 108 msg: err.message 109 }) 110 } else { 111 console.log(createdLog); 112 res.redirect('/logs') 113 } 114}) 115}) 116 117 /* 118Edit 119*/ 120app.get('/logs/:id/edit', (req, res) => { 121 Log.findById(req.params.id, (err, foundLog)=>{ 122 if(err){ 123 res.status(404).send({ 124 msg: err.message 125 }) 126 } else { 127 res.render('Edit', { 128 log: foundLog 129 }) 130 } 131 }) 132}) 133 134/* 135Show 136*/ 137app.get('/logs/:id', (req, res) => { 138 Log.findById(req.params.id, (err, foundLog)=>{ 139 if(err){ 140 res.status(404).send({ 141 msg: err.message 142 }) 143 } else { 144 res.render('Show', { 145 log: foundLog 146 }) 147 } 148 }) 149}) 150 151 app.listen(PORT, () => { 152 console.log('in the building', PORT) 153}) 154
React.js
1const React = require('react'); 2class Edit extends React.Component { 3 render(){ 4 return ( 5 6 <form method="POST" action={`/logs/${this.props.log._id}?_method=PUT`}> 7 title: <input type="text" name="title" defaultValue={this.props.log.title}/><br/> 8 entry: <input type="text" name="entry" defaultValue={this.props.log.entry}/><br/> 9 shipIsBroken: 10 { this.props.log.shipIsBroken 11 ? <input type="checkbox" name="shipIsBroken" defaultChecked /> 12 : <input type="checkbox" name="shipIsBroken"/> 13 } 14 <br/> 15 <input type="submit" value="Submit Changes"/> 16 </form> 17 18 ) 19 } 20} 21module.exports = Edit;
回答1件
あなたの回答
tips
プレビュー