Sails.js 数据未定义

Sails.js Data undefined

我正在尝试创建名为 Edit 函数的函数和更新函数,以使用 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>
 <head>
 <title>Edit Item | Make4You</title>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
 <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
 </head>
 
 
 <body>
 
 <h1 class="">Edit Item</h1>
 <form method="post" action="/item/update?id=<%= items.id %>"> 
  <div class="form-group">
   <label for="title">Title</label>
   <input type="text" name="title" class="form-control" value="<%= items.title %>">
   <label for="category_id">Category</label>
   <select name="category_id">
   <% for (var j=0; j<categories.length; j++){ %>
   <option value="<% if(items[i].category_id == categories[j].id){ %>"><%= categories[j].name %></option>
   <% } %>
   <% } %>
   </select>
   <label for="description">Description</label>
   <input type="text" name="description" class="form-control" value="<%= items.description %>">
   <label for="width">Width</label>
   <input type="text" name="width" class="form-control" value="<%= items.width %>">
   <label for="height">Height</label>
   <input type="text" name="height" class="form-control" value="<%= items.height %>">
   <label for="price">Price</label>
   <input type="price" name="price" class="form-control" value="<%= items.price %>">

  </div>

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

在您的编辑方法中,您似乎正在尝试使用相同的 ID (req.param('id')) 获取 ItemCategory...您可能是想要么传入两个不同的 ID,要么使用附加到获取的 Item 或其他内容的类别 ID...

此外,您对 findfindOne 有点混淆。 find 总是 return 一个结果数组,可能为空。如果找到,findOne 将 return 单个对象,否则 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
});

从您的 returned 错误来看,您可能需要数组。请记住,检查空结果(undefined 或零长度数组)是通过查看 err 参数进行的不同检查 - 空结果不是错误。