定義時に配列内の1つの要素のみを表示しようとしています。NodeJSは単一の配列値を表示します
var UserSchema = new mongoose.Schema({
username: { type: String, unique: true, required: true },
name: { type: String, required: true },
email: { type: String, unique: true, required: true },
password: String,
studio: { type: String, required: true },
comments: [
{
quantity: String,
received: { type: Date, default: Date.now },
collected: { type: String, default: "At Reception" }
}
],
isAdmin: { type: Boolean, default: false }
});
と、次のループでは、すべてのユーザー、自分の電子メール、名前などを一覧表示されます:例えば、私のユーザー・スキーマがある
<% for(var i=0; i < users.length; i++) { %>
<tr>
<td><%= users[i].studio %></td>
<td><%= users[i].name %></td>
<td><%= users[i].email %></td>
<td><%= users[i].comments %></td>
<% } %>
しかしusers[i].comments
は、アレイ全体を表示し、私が唯一の希望をそれはcollected
要素を表示するには - 現在、この表示されます。私は<td><%= users[i].comments.collected %></td>
試してみたが、何でこの結果が示されている
{
received: 2017-12-13T08:51:14.914Z,
collected: 'At Reception',
quantity: '5',
_id: 5a30ea0253432220138b4b89
}
。
編集:私が実際にやりたいことは、収集された値をすべてのユーザーのコメントに表示することです(これをフィルタリングするためにif
ステートメントを使用します)。最初の項目は、あなたのコメントのアレイの上にマッピングすることができ、あなたのコメント配列
の例を含めるために 'comments'配列として、どうすれば 'users [i] .comments.collected'を使うことができますか?あなたの目標は何ですか?どうやら 'comments'配列全体か' comments'配列の最初の要素だけを表示するには? –
こんにちは、すみません、私はこれに新しいです。私はユーザーが入力したコメントをすべて表示したいので、 'At Reception'という収集された値のコメントだけを表示するifステートメントを実行できるようになります。 'collected' –
あなたがそれらのすべてを表示したい場合は、質問の名前を –