在 MEANJS 中为新模块进行身份验证

Authentication in MEANJS for new module

我在处理身份验证的 MEAN 堆栈中遇到问题。

我确实创建了一个名为 "Wallet" 的新模块,这是它的模型:

var WalletSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  bitcoinAddresses: {
    type: Object,
    default: []
  },
  fringe: {
    type: Array
  },
  user: {
    type: Schema.ObjectId,
    ref: 'User',
    required: 'User id cannot be blank'
  }
});

所以它引用了用户 ID,我想在我的页面加载时自动加载用户的钱包。

我的意思是一切正常,除了当我刷新我的页面时,用户被正确加载,但不是导致错误的钱包。

有没有办法在加载用户的同时加载我的钱包?

如果您在电子钱包中设置引用,您应该首先获取电子钱包。这个例子假设你使用猫鼬。

Wallet.findOne({'_id': id}).
 populate({
       path: 'user',
       model: 'User'
}).exec().
then(function (wallet) {//Here is a wallet object with user filled.}).
catch(function(err){//Handle the error});

附带说明:在用户模式中引用钱包可能更好。