javascript - 当我将它传递给客户端时,JSON 对象架构看起来不同

标签 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: 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) 时,我将以下内容打印到终端: terminal result

并且我可以通过引用其索引 hosSchema[0].status 成功访问集合中第一个组件的状态等属性。

下面我尝试将 hosSchema 打印到控制台(在前端)

ipcRenderer.on('requests-results', (event, hosSchema) => {
    console.log(hosSchema)
  })

我得到的结果与他们在终端中看到的不同。下面是图片 result in the client-side

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/

相关文章:

python - 如何改进 Python 中 try 语句的重复使用?

mongodb - 副本集配置无效或者我们不是它的成员,在 kubernetes 中运行

node.js - Mongoose .find 字符串参数

javascript - 我的透视投影矩阵有什么问题?

Javascript地理定位找不到位置信息

javascript - 在保持可排序性的同时更改 JqueryUI 可排序元素的类

JavaScript onclick 事件仅在双击时有效

ios - 将 JSON 数组字符串转换为 JSON 数组返回 null

javascript - 如何从json中打印jquery中的图像

mysql - MySQL 主数据存储和 MongoDB 辅助数据存储之间的 ETL