node.js - 表达 res.json() 丢失数据

标签 node.js express

我正在尝试使用 express 发送数据,但由于某种原因它丢失了部分数据。

function(err, data) {
  if (err) console.log(err);

  console.log(data);
  res.json(data);
}

变量数据看起来像(简化的):

{field1: 'value1', field2: 'value2', field3: {subfield: 'subvalue'}}

但是在浏览器中我收到:

{field1: '值1', field2: '值2', field3: null }

我是不是做错了什么或者遗漏了什么?

更新:

完整代码

UserReadingData.find({
    UserId: {
        "$exists": true,
        "$eq": user._id
    },
    InFuture: false,
    Stopped: false
})
.populate('SeriesId', 'SeriesName')
.exec(function(err, data) {
    if (err) console.log(err);
    console.log(data);
    res.json(data);
});

浏览器中的SeriesId为空。

更新2:

console.log(data)结果:

{ Issues: 
 [ 575efe1c3d6d04662a2cd1c4
   575efe1c3d6d04662a2cd1d3 ],
Stopped: false,
InFuture: false,
__v: 0,
SeriesId: 
 { SeriesName: 'Test name',
   _id: 575efe1b3d6d04662a2cd188 },
UserId: 561c080aa849427a8699cafd,
_id: 575f0981ddac1c802d9d1536 }

更新3:

我的架构

var seriesSchema = mongoose.Schema({
Meta: metaInformationSchema,
SeriesName: String,
Description: String,
Creators: [creatorsSchema],
Featured: [featuredSchema],
PublisherName: String,
IsStoryArc: Boolean,
Issues: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Issue'
}]}, {
collection: 'library'});

var userReadingDataSchema = mongoose.Schema({
UserId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
},
SeriesId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Series'
},
Issues: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Issue'
}],
InFuture: Boolean,
Stopped: Boolean}, {collection: 'readingdata'});

我是从sql出来的,这是我第一次使用mongo。

最佳答案

我不知道问题的真正原因,但我从集合 readingdata 中删除了所有内容,并用一些新数据填充了它。之后 res.json 正常工作。

关于node.js - 表达 res.json() 丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798446/

相关文章:

javascript - 访问本地主机:3000 through localhost/myproject/index. html

api - 使用Axios进行API调用时Express中的错误处理

node.js - 如何使用package.json保存全局包的安装?

node.js - 无法从 AWS sdk [S3] 中捕获错误

javascript - Tensorflow.js 使用 Node 保存模型

javascript - Node JS 中的多重查询

javascript - 任何客户端 JavaScript 框架都可以与 Node.js+Express.js+socket.io.js 很好地集成吗?

javascript - Node.js 和 Express.js 错误 : Can't set headers after they are sent

node.js - 如何强制将用户路由到 HTTP

node.js - 快速路线无法运行