Trying to combine a client and an account collection.
When I look use a res.send(client), I only get the account id's. I am not sure how to populate the fields to push account info into clients. I have seen some one to many solutions, but can't figure out this two way thing.
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