Javascript 当我将JSON对象体系结构传递到客户端时,它看起来不同
这是我的模式Javascript 当我将JSON对象体系结构传递到客户端时,它看起来不同,javascript,json,mongodb,mongoose,electron,Javascript,Json,Mongodb,Mongoose,Electron,这是我的模式 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var messageSchema = new Schema({ requestNumber: String, requestedDateTime: String, reasons: String, state: String, hospital: String, phone:
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
打印到控制台(在前端)
我得到的结果与他们在终点站看到的不同。下面是图片
和hosSchema[0]。状态返回未定义
我的问题是:
1) 为什么hosSchema[0]。状态在前端不起作用
2) 访问客户端中属性的正确方法是什么?前端需要做的就是使用hosSchema[0]。\u doc.status
而不是hosSchema[0]。status
ipcRenderer.on('requests-results', (event, hosSchema) => {
console.log(hosSchema)
})