使用populate时出错,适用于localhost,但是在heroku mongolab上有错误

时间:2017-01-10 12:06:16

标签: javascript mongodb heroku mongoose

errCastError:对于模型“Item”,路径为“_id”的值为“5874b2ee859d3000127b95f1”的对象导入为ObjectId失败

var userSchema = new mongoose.Schema({
  uid: String,
  email: String,
  name: String,
  lastname: String,
  reset: Boolean,
  cap: Number,
  verified: Boolean,
  inventory: [{type: mongoose.Schema.Types.ObjectId, ref: 'Item'
  }]
});
var itemSchema = new mongoose.Schema({
  name: String,
  email: String,
  description: String,
  youtube: String,
  sent: Boolean,
  date: Date
});

var Item = mongoose.model('Item', itemSchema);

我使用firebase来管理用户,所以我需要firebase uid。

router.get('/my', function(req, res, next) {
  User.findOne({uid: req.user.uid}).populate('inventory').exec(function(err, user){
    if(err) console.log('asda',err);
    console.log('user is',user);
    res.send(user.inventory);
  });
});

以下是来自mongolab的用户和项目。

{
    "_id": {
        "$oid": "5874a9fb53377a0012fe83db"
    },
    "uid": "89ed4720-cea6-4f0d-bcd0-4fc1f4a0bf2b",
    "email": "hey@heyhey.edu",
    "name": "Kaan",
    "lastname": "Kocturk",
    "reset": false,
    "verified": true,
    "cap": 2,
    "inventory": [
        {
            "$oid": "5874b2ee859d3000127b95f1"
        }
    ],
    "__v": 1
}

{
    "_id": {
        "$oid": "5874b2ee859d3000127b95f1"
    },
    "name": "kaan",
    "email": "kkocturk1@bbb.edu",
    "description": "HAAHAHAHAHHAHAHAHAHAH",
    "youtube": "",
    "date": {
        "$date": "2017-01-13T00:00:00.000Z"
    },
    "sent": false,
    "__v": 0
}

这是我的第一个制作网站,我自己也不够好解决这个问题。我看了其他解决方案,但无法弄明白......请帮忙!谢谢!

0 个答案:

没有答案