在模型内创建子文档并将其添加到数组中

时间:2019-04-20 04:15:39

标签: javascript arrays node.js mongoose mongoose-schema

我正在接受identityResponse的响应,该响应为我提供了一个“帐户”数组,然后我需要运行一个for循环来创建每个帐户。对于每个帐户,我还需要将User文档作为子文档数组放入。我似乎不知道该怎么做。我已经有了它来创建帐户,但是它不会推送到用户模型中。

我已经尝试过findOneAndUpdate,让newAccount = new PlaidUserAccounts,尝试过一个发送结果的承诺,然后将其推送到User.plaidAccounts中。

也许我可以直接在User Profile中创建它,同时在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;

0 个答案:

没有答案