Я принимаю ответ от identityResponse, который предоставляет мне массив «учетных записей», которые затем мне нужно запустить цикл for для создания каждой учетной записи. Для каждой учетной записи мне также нужно вставить модель пользователя в виде массива вложенных документов. Я не могу понять, как это сделать. Я получил его для создания учетной записи, но он не будет добавлен в модель пользователя.
Я попытался найти findOneAndUpdate, let newAccount = new PlaidUserAccounts, попробовал обещание отправить результат, а затем отправить его в User.plaidAccounts.
Может быть, я могу создать непосредственно в профиле пользователя, и он будет одновременно создавать его в PlaidUserAccounts? Или он создаст его только внутри пользователя?
app.get('/updateUser', function (request, response, next) {
client.getIdentity(ACCESS_TOKEN, function (error, identityResponse) {
if (error != null) {
prettyPrintResponse(error);
return response.json({
error: error,
});
}
// CREATE ACCOUNTS
for (let i = 0; i < identityResponse.accounts.length; i++) {
db.PlaidUserAccounts.create({
userID: response._id,
accessToken: ACCESS_TOKEN,
account_id: identityResponse.accounts[i].account_id,
accountName: identityResponse.accounts[i].name,
official_name: identityResponse.accounts[i].official_name,
availableBalance: identityResponse.accounts[i].balances.available,
mask: identityResponse.accounts[i].mask,
type: identityResponse.accounts[i].type,
subtype: identityResponse.accounts[i].subtype,
});
}
// Push into User Profile
})
}
let userSchema = new Schema({
//user id auto gen by mongo
name: {
type: String,
trim: true,
required: true
},
// This is the password stored that comes from Auth0
password: {
type: String,
trim: true,
required: true
},
// This is the email stored that comes from Auth0
email: {
type: String,
unique: true,
required: true
},
phoneNum: {
type: String
},
profilePicture: {
type: String
},
// wishList is an array of objects. The object it accepts is model wishItem.js.
wishList: [WishItem],
// plaidItems is an array of objects. The object it accepts is model plaidItems.js
// In Plaid an Item is a Bank or Institution. Each Item had its own Access_Token and Item_ID.
// A User can have multiple Items(Accounts) connected.
plaidItems: [PlaidItems],
// plaidAccounts is an array of objects. The object it accepts is model plaidAccounts.js
// In Plaid and Item(Bank Institution) can have multiple Accounts.
plaidAccounts: [PlaidUserAccounts],
// ACHAuth is an array of objects. The object it accepts is model plaidACHAuth.js
plaidACHAuth: [PlaidACHAuth],
// Stripe Created Customer Profile
stripeCustomer: [StripeCustomer],
// deposits is an array of objects. The object it accepts comes from stripeDeposits.js
// These are records of the COMPLETED deposits.
deposits: [StripeDepos],
rounded: [RoundedTrans],
currentBalance: {
type: Number,
trim: true
},
// withdrawalSuccess is an array of objects. The object it accepts comes from stripeWithdrawal.js
// These are records of the COMPLETED withdrawals to the user.
withdrawalSuccess: [StripeWithdrawal],
// withdrawalRequest is an array of objects. The object it accepts comes from withdrawRequest.js
// These are records of the REQUESTED withdrawals by the user.
withdrawalRequest: [WithdrawRequest],
// Date/Timestamp of User's initial registration.
createDate: {
type: Date,
default: Date.now
}
});
let User = mongoose.model("User", userSchema)
module.exports = User;