2017-12-08 4 views
0

私はsails.jsを使ってデータベース内のアイテムを更新するためにEdit関数とUpdate関数を作成しようとしていますが、長さは未定義のプロパティを読み取ることができません。私はちょっとここにこだわっている、助けてください。あなたの編集方法でSails.jsデータが定義されていません

This is a picture of Edit and Update functions

This is an error result that I got when I click on Edit button

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t <title>Edit Item | Make4You</title> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> 
 
\t <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> 
 
\t <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script> 
 
\t <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> 
 
\t </head> 
 
\t 
 
\t 
 
\t <body> 
 
\t 
 
\t <h1 class="">Edit Item</h1> 
 
\t <form method="post" action="/item/update?id=<%= items.id %>"> \t 
 
\t \t <div class="form-group"> 
 
\t \t \t <label for="title">Title</label> 
 
\t \t \t <input type="text" name="title" class="form-control" value="<%= items.title %>"> 
 
\t \t \t <label for="category_id">Category</label> 
 
\t \t \t <select name="category_id"> 
 
\t \t \t <% for (var j=0; j<categories.length; j++){ %> 
 
\t \t \t <option value="<% if(items[i].category_id == categories[j].id){ %>"><%= categories[j].name %></option> 
 
\t \t \t <% } %> 
 
\t \t \t <% } %> 
 
\t \t \t </select> 
 
\t \t \t <label for="description">Description</label> 
 
\t \t \t <input type="text" name="description" class="form-control" value="<%= items.description %>"> 
 
\t \t \t <label for="width">Width</label> 
 
\t \t \t <input type="text" name="width" class="form-control" value="<%= items.width %>"> 
 
\t \t \t <label for="height">Height</label> 
 
\t \t \t <input type="text" name="height" class="form-control" value="<%= items.height %>"> 
 
\t \t \t <label for="price">Price</label> 
 
\t \t \t <input type="price" name="price" class="form-control" value="<%= items.price %>"> 
 

 
\t \t </div> 
 

 
\t \t 
 
\t \t <input type="submit" value="submit"> 
 
\t </form> 
 
\t </body> 
 
</html>

答えて

0

あなたは同じid(req.param('id'))を使用してItemCategoryの両方を取得しようとしているように、それが見えます。 ..おそらく、2つの異なるIDを渡すか、取り出されたIDに関連付けられたカテゴリIDを使用することを意味しますItemまたは何か...

また、findfindOneの間に少し混乱があります。 findは常に結果の配列を返します。空の場合もあります。 findOneが見つかった場合、単一のオブジェクトを返す、またはundefinedないになります多分あなたは、配列をしたいよう

Category.find({id: category_id}).exec(function(err, results) { 
    // results is an array, possibly empty 
}); 

Category.findOne({id: category_id}).exec(function(err, results) { 
    // results is either a model or undefined 
}); 

ビューから返されるエラーから、それが見えます。そして、空の結果(undefinedまたは長さがゼロの配列)をチェックすると、errパラメータを見ても慎重にチェックすることに注意してください。空の結果はエラーではありません。

+0

私はCategory.find()。exec(finction(err、result){}を変更しても動作しますか? –

+0

@MaxKasemそれはあなたに長さのエラーを与えるのをやめます。あなたが探しているものが何であるかを知ることができます。あなたが最初の部分も読んでいることを確認してください。あなたのアイテムとあなたのカテゴリー。 – arbuthnott

+0

はい前にそれを知らなかった。ありがとう! –

関連する問題