我在 MEAN 堆栈中遇到了处理身份验证的问题。
事实上,我已经创建了一个名为"钱包"的新模块,这是它的模型:
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});
作为旁注:最好在用户架构中引用钱包。