Can’t get mongodb to populate information I am trying to push between collections

I am working on a mongodb project, I am trying to connect clients with their accounts. When I create a new account to go under the client, none of the information added shows up on the redirect page. Just blank spaces. When I look use a res.send(client), which I think would show both the new account and the related client I only get the account id's. I think this is the problem, but I am not sure how to populate the fields to push into clients.

Here's what I get:

{"accounts":["5fba8c2f8e5610374463caab","5fba8d49ae11d93c40ce08d9","5fba8dcfb4192a22d469eeef"],"_id":"5fba8bf1407032134c4c22b4","company":"Testing2","contact":"Testing2","phone":5555,"email":"Testing2","city":"Testing2","state":"Testing2","zip":5555555,"__v":3}

This is the routing I am using (based on a course that seems to work fine for them(?)):

app.post('/clients/:id/accounts', async (req, res) => {
    const { id } = req.params;
    const client = await Client.findById(id);
    const { name, location, tid } = req.body;
    const account = new Account({ name, location, tid })
    client.accounts.push(account);
    account.client = client;
    await account.save();
    await client.save();
    res.send(client);
    // res.redirect(`/clients/${client._id}`)
    
})

Here are the two models I am working with: Clients:

   const mongoose = require('mongoose');
   const { Schema } = mongoose;

   const ClientSchema = new Schema({
   company: String,
   contact: String,
   phone: Number,
   email: String,
   city: String,
   state: String,
   zip: Number,
   accounts: [
    {
        type: Schema.Types.ObjectId,
        ref: 'Account',
    },
   ]
   });

   module.exports = mongoose.model('Client', ClientSchema);

And the account model:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const AccountSchema = new Schema({
    name: String,
    location: String,
    client: [{
        type: Schema.Types.ObjectId,
        ref: 'Client'
    }],
    description: String,
    contact: String,
    tid: String,
    loader: String,
    surcharge: Number,
    payout: Number,
    net: Number,
})

module.exports = mongoose.model('Account', AccountSchema);

Any help is greatly appreciated. Thanks in advance!!

SonOfZappa



Read more here: https://stackoverflow.com/questions/64956475/cant-get-mongodb-to-populate-information-i-am-trying-to-push-between-collection

Content Attribution

This content was originally published by SonofZappa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: