我正在尝试从嵌套的JSON对象访问数据而没有PUG和product.highlights [0]中的任何javascript。标题需要循环索引。
JSON格式
[{"id":"0.33454785604755677","title":"Product 2 Title",
"highlights":[
- {"heading":"Product 2 Heading 1","description":"Product 2 Description 1"},
- {"heading":"","description":""},
- {"heading":"","description":""},
- {"heading":"","description":""}]
}]
我试过了
-var x = [0,1,2,3,4,5]
-var high =[product.highlights[0].heading,product.highlights[1].heading,product.highlights[2].heading,product.highlights[3].heading]
each val in x
-var p = high[x] // here its not considering 'x'
h3 #{p}
并直接使用循环
each i in [0,1,2,3,4,5]
h3 #{product.highlights[i].heading} //but here its not considering 'i'
它在'name','for'里面工作。
但我需要它在里面工作:
- value=(editing ? product.highlights[0].heading:'')
each i in [1, 2, 3, 4, 5, 6]
.form-control
label(for="highlights[" + i +"][heading]") Highlights Number #{i} Heading
input(type="text",name="highlights[" + i +"][heading]",value=(editing ? product.highlights[0].heading:''))
我不知道如何通过哈巴狗的JavaScript访问它
谁能告诉我怎么能这样做?
-function productHeading(product,y){return product.highlights[y].heading;}
each i in [0, 1, 2, 3, 4, 5]
.form-control
label(for="highlights[" + i +"][heading]") Highlights Number #{i+1} Heading
input(type="text",name="highlights[" + i +"][heading]",value=(editing ? productHeading(product,i):''))
product存储findById方法找到的对象,该方法稍后发送到pug文件
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const SubProductSchema = new Schema({
product_id: Schema.Types.ObjectId,
title: String,
name:{
type: String,
unique: true,
required: true
},
price:Number,
image:String,
alt: String,
quantity:Number,
description:String,
bgImage:String,
primaryImage:String,
primaryAlt: String,
primaryHeading:String,
primaryDescription:String,
highlights:[{
heading:String,
description:String,
icon:String
}],
specs:[{
heading:String,
description:String,
image:String
}],
featured:Boolean,
preOrder:Boolean,
video:Boolean
});
module.exports = mongoose.model('xxx',xxx);
控制器文件
const Product = require('../models/product');
.
.
Product.findById(prodId)
.then( product => { ....
等等 。 编辑是一个布尔变量,如果它是真的,则调用pug文件中的productHeading函数并将值初始化为它,否则将其设置为''(空)