Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all!
So if there will be team id i will find that team if there will not be team id then user must create new team then in create team members user must add multi members and there i will get team.id so then user must create training so whats i'm doing wrong here? i'm new in this so this is very hard for me so can someone please help me?

What I have tried:

public async store({ request, response }: HttpContextContract) {
        // find or create team
        let team = Team;
        if (request.params.existingTeamId) {
            team = Team.findOrFail(request.params.existingTeamId)
        } else {
            team = Team.create({ title: request.params.addTeam.title, size: request.params.addTeam.size, teamlead_first_name: request.params.teamLead.firstName, teamlead_last_name: request.params.teamLead.lastName, teamlead_email: request.params.teamLead.email, teamlead_id: request.params.teamLead.id });
        }
        // create teammembers
        request.params.teamMembers.forEach(member => TeamMember.create({ ...member, name: teamMembers.name, position: teamMembers.position, email: teamMembers.email, team_id: team.id,  }))
        // create training
        const training = Training.create({ title: request.params.traning.title, body: request.params.training.body  });

        return response.json([team, training]);
    }
Posted
Updated 22-Dec-21 1:34am
Comments
OriginalGriff 22-Dec-21 6:30am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15476294 22-Dec-21 7:24am    
I changed it to
public async create({ request, response }: HttpContextContract) {
// find or create team
let team: typeof Team
let r = request.body
// TODO validate `r` against schema
if (r.existingTeamId) {
team = await Team.findOrFail(r.existingTeamId)
} else {
team = await Team.create({
title: r.title,
size: r.size,
})
}
// create teammembers
r.teamMembers.forEach(
async (member) =>
await TeamMember.create({
...member,
name,
position,
email,
teamId: team.id,
})
)
// create training
const training = await Training.create({ title: r.title, body: r.training.body })

// create teamLead
const teamLead = Team.create({ teamlead_first_name: r.firstName, teamlead_last_name: r.lastName, teamlead_email: r.email, teamlead_id: r.teamLeadId })

return response.json(training)
}
and i'm getting error:

insert into "teams" ("created_at", "size", "title", "updated_at") values ($1, DEFAULT, DEFAULT, $2) returning "id" - null value in column "title" of relation "teams" violates not-null constraint

1 solution

Quote:
i'm getting error:

insert into "teams" ("created_at", "size", "title", "updated_at") values ($1, DEFAULT, DEFAULT, $2) returning "id" - null value in column "title" of relation "teams" violates not-null constraint

We can't do anything about that: your DB "Title" column is set to "do not allow nulls" and you are passing a null value. That presumably mean you have to find a value for it from somewhere, and we have no idea where that should come from!

So if your DB requires not-null values, they you need to find where such data comes from ... we can't do that for you!
 
Share this answer
 
Comments
Member 15476294 22-Dec-21 7:39am    
I don't understand i'm giving values for title and size with postman so it's not null yes? {
"addTeam": { "title": "SIEMENS Marketing Team", "size": 50 },
"teammembers": [
{ "name": "Maria Romanova", "email": "maria@gmail.com", "position": "Marketing Head" },
{ "name": "John Doe", "email": "john@gmail.com", "position": "Developer" }
],
"teamlead": { "firstName": "George", "lastName": "Thompson", "email": "george@gmail.com" },
"training": { "title": "Your DIC Survey", "description": "Great survey with great results! Start answering today!" }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900