大変初歩的な内容で恐縮でしうが、どなたかお教えいただけると大変助かります (冗長な質問で申し訳ありません。。。)
1 MongoDBにおいて、二つのデータベースの連携させようとしています。例として、「フルーツ」コレクションと「ノート」コレクションを下記のようなスキーマで用意しています。
<Fruit> ```node.js//SCHEMA
var fruitSchema = new mongoose.Schema({
name: String,
origin: String,
note:[
{
type: mongoose.Schema.Types.ObjectId,
ref: "Note"
}
]
});
module.exports = mongoose.model("Fruit", fruitSchema);
<Note> ```node.js var mongoose = require("mongoose"); //SCHEMA var noteSchema = mongoose.Schema({ note: String, author: String, }); module.exports = mongoose.model("Note", noteSchema);
2 フルーツの方に、ノートのデータを格納するフォームを表示させようとして次のようなコードを記述いたしました。
node.js
1//NEW - show form to create new portfolio company 2router.get("/new", function(req,res){ 3 Fruit.findById(req.params.id, function(err,fruit){ 4 if(err){ 5 console.log(err); 6 }else{ 7 res.render("notes/new",{fruit: fruit}); 8 } 9 }) 10});
3 フォームは、下記のようにしているのですが、
node.js
1<% include ../partials/header %> 2 3<div class="container"> 4 <div class =row> 5 <h1 style="text-align: center;">Add a Note </h1> 6 <div style="width: 30%; margin: 25px auto;"> 7 <form action="/fruits/<%= fruit._id %>/notes" method="POST"> 8 9 <div class="form-group"> 10 <input class="form-control" type = "text" name = "note[note]" placeholder="note"> 11 </div> 12 13 <div class="form-group"> 14 <input class="form-control" type = "text" name = "note[author]" placeholder="author"> 15 </div> 16 17 <div class="form-group"> 18 <button class="btn btn-lg btn-primary btn-block">Submit!</button> 19 </div> 20 21 </form> 22 <a href="/fruits">Back to the Fruits</a> 23 24 </div> 25 </div> 26 27</div> 28 29<% include ../partials/footer %>
4 下記のようなエラーが出てしまい進みません。。。。
node.js
1TypeError: /home/ubuntu/workspace/Fruits/views/notes/new.ejs:7 2 5| <h1 style="text-align: center;">Add a Note </h1> 3 6| <div style="width: 30%; margin: 25px auto;"> 4 >> 7| <form action="/fruits/<%= fruit._id %>/notes" method="POST"> 5 8| 6 9| <div class="form-group"> 7 10| <input class="form-control" type = "text" name = "note[note]" placeholder="note"> 8 9Cannot read property '_id' of null 10 at eval (eval at compile (/home/ubuntu/workspace/Fruits/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:22:31) 11 at returnedFn (/home/ubuntu/workspace/Fruits/node_modules/ejs/lib/ejs.js:653:17) 12 at tryHandleCache (/home/ubuntu/workspace/Fruits/node_modules/ejs/lib/ejs.js:251:36) 13 at View.exports.renderFile [as engine] (/home/ubuntu/workspace/Fruits/node_modules/ejs/lib/ejs.js:482:10) 14 at View.render (/home/ubuntu/workspace/Fruits/node_modules/express/lib/view.js:135:8) 15 at tryRender (/home/ubuntu/workspace/Fruits/node_modules/express/lib/application.js:640:10) 16 at EventEmitter.render (/home/ubuntu/workspace/Fruits/node_modules/express/lib/application.js:592:3) 17 at ServerResponse.render (/home/ubuntu/workspace/Fruits/node_modules/express/lib/response.js:1008:7) 18 at /home/ubuntu/workspace/Fruits/routes/notes.js:12:14 19 at /home/ubuntu/workspace/Fruits/node_modules/mongoose/lib/model.js:4527:16 20 at Query._completeOne (/home/ubuntu/workspace/Fruits/node_modules/mongoose/lib/query.js:1883:12) 21 at Immediate.Query.base.findOne.call (/home/ubuntu/workspace/Fruits/node_modules/mongoose/lib/query.js:1943:10) 22 at Immediate.<anonymous> (/home/ubuntu/workspace/Fruits/node_modules/mquery/lib/utils.js:116:16) 23 at runCallback (timers.js:672:20) 24 at tryOnImmediate (timers.js:645:5) 25 at processImmediate [as _immediateCallback] (timers.js:617:5)
解消するために色々試してみたのですが、どなたか解消方法をお教えいただけないでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。