这是我的架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var messageSchema = new Schema({
requestNumber: String,
requestedDateTime: String,
reasons: String,
state: String,
hospital: String,
phone: String,
status: {type: String, default: 'Pending'},
latestUpdate: Date,
createdAt: {type: Date, default: Date.now}
});
module.exports = mongoose.model('Requests', messageSchema);
下面我将返回包含三个组件的集合
ipcMain.on('load-requests', function(event) {
hosSchemaModel.find(function(err, hosSchema) {
if (err) {
console.log('inside error') // return res.send(err);
} else {
event.sender.send('requests-results', hosSchema) // this line of code passes hosSchema to the client side
console.log(hosSchema[0].state) //prints the state attribute of the first component in the collection without any errors.
}
});
});
当我尝试在服务器中使用 console.log(hosSchema)
时,我将以下内容打印到终端:
并且我可以通过引用其索引 hosSchema[0].status
成功访问集合中第一个组件的状态等属性。
下面我尝试将 hosSchema
打印到控制台(在前端)
ipcRenderer.on('requests-results', (event, hosSchema) => {
console.log(hosSchema)
})
和hosSchema[0].status
返回undefined。
我的问题是:
1) 为什么 hosSchema[0].status
在前端不起作用?
2) 在客户端访问属性的正确方法是什么?
最佳答案
在前端你所要做的就是使用hosSchema[0]._doc.status
而不是hosSchema[0].status
关于javascript - 当我将它传递给客户端时,JSON 对象架构看起来不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605216/