Loopback 4: Transmitting associated information

In my registration form, I have the option to add one or more addresses for a user. In order to streamline this process, I am using loopback:

{
  "id": 0,
  "name": "User Name",
  "addresses": [
    {
      "street": "xx",
      "city": "xx",
      "country": "xx"
    },
    {
      "street": "yy",
      "city": "yy",
      "country": "yy"
    }
  ]
}

Within my User model (which also includes an Address model), I have defined:

export class User extends Entity {
  @property({
    type: 'number',
    id: true,
    required: true,
  })
  id: number;

@property({
    type: 'string',
    required: true,
  })
  name: string;

 @hasMany(() => Address, {keyTo: ‘user_id’})
  Addresses: Array<Address>;
}

Additionally, in UserRepository, I have specified:

this.addresses = this.createHasManyRepositoryFactoryFor(
      'addresses',
      AddressRepositoryGetter,
    );

Upon submitting the JSON data, loopback returns the following error message:

{
  "error": {
    "statusCode": 422,
    "name": "ValidationError",
    "message": "The `user` instance is not valid. Details: `addresses` is not defined in the model (value: undefined).",
    "details": {
      "context": "user",
      "codes": {
        "addresses": [
          "unknown-property"
        ]
      },
      "messages": {
        "addresses": [
          "is not defined in the model"
        ]
      }
    }
  }
}

It seems that the "addresses" relation is not recognized as a property of the model. How can I resolve this issue? I am aware that I could make a separate request to save the addresses, but I would prefer to avoid that solution.

Answer №1

The datasource juggler library is responsible for throwing this error, indicating that the addresses column is not defined in your User table within the database. Please ensure to verify this.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Caution: The `id` property did not match. Server: "fc-dom-171" Client: "fc-dom-2" while utilizing FullCalendar in a Next.js environment

Issue Background In my current project, I am utilizing FullCalendar v5.11.0, NextJS v12.0.7, React v17.0.2, and Typescript v4.3.5. To set up a basic calendar based on the FullCalendar documentation, I created a component called Calendar. Inside this comp ...

What is the best way to send an enum from a parent component to a child component in

I'm trying to send an enum from a parent component to a child component. This is the enum in question: export enum Status { A = 'A', B = 'B', C = 'C' } Here's the child component code snippet: @Component({ ...

The module called "discord.js" does not have a component named "Intents" available for export

Issue with importing module '"discord.js"' - 'Intents' not exported. Here is the code in question: import dotenv from 'dotenv'; const bot = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents. ...

Concealing Dropdown Box Information Based on a Customized List in Angular

Is there a way to remove an item from a dropdown list once it has been selected? For example, if I have a dropdown box with options 'Dog', 'Lion', and 'Cat', how can I make it so that once I select 'Dog', it will no ...

What is the best way to convert an Express route to Typescript?

I'm trying to create a wrapper called loginRequired for my Express route, but I'm struggling to define the right types. This is what my loginRequired wrapper looks like: export const loginRequired = (fn) => (req, res, next) => { // Ch ...

Exploring ways to retrieve a function-scoped variable from within an Angular subscribe function

Here's the scenario: I have a simple question regarding an Angular component. Inside this component, there is a function structured like this: somethingCollection: TypeSomething[] ... public deleteSomething(something: TypeSomething): void { // so ...

Typescript fill() function like Laravel's fill()

One interesting feature in Laravel is the fill() method of every database model, which only assigns fields marked as "mass assignable" to the class property. (https://laravel.com/docs/5.8/eloquent#mass-assignment) public class Rectangle { protected $f ...

Showing an error message upon submission in Angular 4 according to the server's response

Struggling for hours to display an error message when a form submits and returns an error status code. The solution seems elusive... In the login form component below, I've indicated where I would like to indicate whether the form is valid or invalid ...

Tips for transforming promise function into rxjs Observables in Angular 10

As a beginner in typescript and angular, I am trying to understand observables. My query is related to a method that fetches the favicon of a given URL. How can I modify this method to use observables instead of promises? getFavIcon(url: string): Observ ...

Having trouble creating a unit test for exporting to CSV in Angular

Attempting to create a unit test case for the export-to-csv library within an Angular project. Encountering an error where generateCsv is not being called. Despite seeing the code executed in the coverage report, the function is not triggered. Below is the ...

Angular select elements connected through data binding

I am working on selecting values in an Angular application. Specifically, I want to establish a connection between two select elements so that changing the value of one will also change the value of the other. <select class="form-control" id="example ...

Customizing form validation in React using Zod resolver for optional fields

I am currently working on creating a form using React-hook-form and zod resolver. My goal is to have all fields be optional, yet still required despite being marked as optional in the zod schema: const schema = z.object({ name: z.string().min(3).max(50 ...

How can one effectively import and save data from a CSV file into an array comprised of objects?

I am looking to read a CSV file and store it in a variable for future access, preferably as an array of objects. However, when trying the following code snippet: const csv = fs .createReadStream('data.csv') .pipe(csv.default({ separator: &ap ...

Solving automatically generated TypeScript MongoDB types for GraphQL results

Utilizing the typescript-mongodb plugin along with graphql-codegen to automatically generate Typescript types enables easy data retrieval from MongoDB and GraphQL output via Node. The initial input schema in GraphQL format appears as follows: type User @ ...

We are in need of a provider for the Ionic Network native plugin

I have encountered an issue while trying to use Ionics native plugin "Network" as it fails due to a missing provider. To prevent any errors, I performed a fresh installation of Ionic along with the necessary dependencies: ionic cordova plugin add cordova- ...

The data type 'string' cannot be assigned to the type 'SystemStyleObject | undefined' in the context of Next.js and Chakra UI

I am currently working on a project that involves Next.js, TypeScript, and Chakra UI. While configuring Button themes in the button.ts file, I encountered an error related to the baseStyle object for properties like borderRadius, color, and border: Type & ...

An array containing optional types Ts and required types Ks

Can an array be typed in TypeScript to require at least one or more strings at any index, with any extra elements being numbers? type NumberArrayWithAtleastOneString = [...(number[] | string)[], string] const a: NumberArrayWithAtleastOneString = [1,' ...

The consequences of exporting raw .ts files instead of .d.ts files for module type declarations

In a typical TypeScript module, you will find a 'src' directory containing TypeScript source code files. The module compiles these sources using 'tsc -d --outDir dist' to create the 'dist' directory and sets specific package m ...

Exploring techniques to compare two objects in JavaScript and then dynamically generate a new object with distinct values

var person1={ name:"Sarah", age:"35", jobTitle:"Manager" } var person2={ name:"Sarah Sanders", age:"35", jobTitle:"Manager" } //the name value has been updated in the above object, s ...

TypeScript is unable to identify the property causing the error

Recently, I came across a new feature in JavaScript where you can add a cause to the Error constructor. However, when attempting to utilize this feature in my application, it fails to start due to not recognizing this additional argument in the constructo ...