在节点JS EJS文件中的数组中调用对象
大家干得好!
我有一个用 Node js 编写的项目。 在这个项目中,我的模式结构如下。
const debt = new Schema({
name: {
type: String,
required: true,
},
medicine: [{
data: {
type: Schema.Types.ObjectId,
ref: 'Medicine',
},
quantity: {
type: Number,
default: 1
}
}],
total: {
type: Schema.Types.Decimal128,
default: 0,
},
status: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});
const medicine = new Schema({
name: {
type: String,
unique: true,
required: true,
},
price: {
type: Schema.Types.Decimal128,
required: true,
},
medicineType: {
type: String,
required: true,
},
description: {
type: String,
required: true,
trim: true,
},
image: {
type: String,
},
createdAt: {
type: Date,
default: Date.now,
},
});
我能够从邮递员成功地将数据添加到此架构中。 但是我无法读取ejs文件中的数据,我该怎么办?
我可以通过控制台读取数据,我在控制台上使用的代码如下:
const debt = await Debt.find({}).populate('medicine.data').sort('-createdAt');
console.log(JSON.stringify(debt[0].medicine[0].data));
上面的代码成功返回了它引用的模式和它自己的数据。
<% for(let i=0; i< debt.medicine.length; i++) { %>
<input type="checkbox" id="<%= medicine[i]._id%>" name="medicine" checked value="<%= debt.medicine[i].data%>">
<label for="<%= medicine[i]._id%>"><%= JSON.stringify(debt.medicine[i].data) %> - Select %> </label>
<% } %>
但是当我在 ejs 文件中执行此操作时,它仅返回 id 和数量值。当我在控制台中打印它时,我可以访问医学模式中的所有数据。 为什么我无法访问 ejs 文件?你能帮助我吗?
当我在控制台中打印它时它返回的数据
{"_id":"624e0b3633533f3a31e3dc6a","name":"Arveles","price":{"$numberDecimal":"19"},"medicineType":"Pill","description":"Medicine","image":"/uploads/1649281846287-medicine1.jpeg","createdAt":"2022-04-06T21:50:46.294Z","__v":0}
当我在 ejs 上打印它时它返回一个像这样的值
{"data":"624e0b3633533f3a31e3dc6a","quantity":2,"_id":"62537484a05359a8cf356fef"}
Good work everyone!
I have a project that I wrote with Node js.
In this project, I have a schema structure as follows.
const debt = new Schema({
name: {
type: String,
required: true,
},
medicine: [{
data: {
type: Schema.Types.ObjectId,
ref: 'Medicine',
},
quantity: {
type: Number,
default: 1
}
}],
total: {
type: Schema.Types.Decimal128,
default: 0,
},
status: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});
const medicine = new Schema({
name: {
type: String,
unique: true,
required: true,
},
price: {
type: Schema.Types.Decimal128,
required: true,
},
medicineType: {
type: String,
required: true,
},
description: {
type: String,
required: true,
trim: true,
},
image: {
type: String,
},
createdAt: {
type: Date,
default: Date.now,
},
});
I am able to successfully add data to this schema from postman.
But I can't read the data in the ejs file, how can I do that?
I can read the data through the console and the code I use on the console is as follows:
const debt = await Debt.find({}).populate('medicine.data').sort('-createdAt');
console.log(JSON.stringify(debt[0].medicine[0].data));
The above code successfully returns both the schema it references and its own data.
<% for(let i=0; i< debt.medicine.length; i++) { %>
<input type="checkbox" id="<%= medicine[i]._id%>" name="medicine" checked value="<%= debt.medicine[i].data%>">
<label for="<%= medicine[i]._id%>"><%= JSON.stringify(debt.medicine[i].data) %> - Select %> </label>
<% } %>
But when I do this in the ejs file, it only returns the id and quantity values. When I print it in the console, I can access all the data as in the medicine schema.
Why can't I access the ejs file? can you help me?
The data it returns when I print it in the console
{"_id":"624e0b3633533f3a31e3dc6a","name":"Arveles","price":{"$numberDecimal":"19"},"medicineType":"Pill","description":"Medicine","image":"/uploads/1649281846287-medicine1.jpeg","createdAt":"2022-04-06T21:50:46.294Z","__v":0}
When I print it on ejs it returns a value like this
{"data":"624e0b3633533f3a31e3dc6a","quantity":2,"_id":"62537484a05359a8cf356fef"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我刚刚解决了这个情况,我没有通过控制结构正确填充页面......
也许其他人可能会遇到这种情况,在这种结构中,需要在填充过程中发送两个数据。
问题解决:
可以说整个流程都围绕着这里。
Yes, I just solved the situation, I did not correctly populate the page through the control structure...
Maybe it may happen to others, in such structures, it is necessary to send two data inside the populate process.
Solution to the problem:
I can say that the whole process revolves here.