Search a collection reference through a smart field value not the _id

I have two collections. One has an item A referenced to another item B in another collection.

I have to do a manual search from the edit view of the first item A to match the other item B.

Because it’s a mongoose reference, in the widget settings, I choosed the dropdown with search enabled.

For the item B (the reference) I’ve created a smart field called “Reference” (it contains a firstname, lastname, price, blablabla).

Expected behavior

In the first item A (edit view), I would be able to search the second item B through its “Reference” smart field, not the _id field.

Actual behavior

When I type a word or just letters that should be contained in the “Reference” smart field of the item B, it doesn’t work. If I type a part of the_id field , it works.

Hi @XavierColombel,

Did you create the smart field Reference in order to fix your original issue ?

The dropdown widget (with or without search) uses the referenced field to display it’s values


Using it with the dropdown widget, I’m able to search for a reference directly via it’s name
image

This should also work on a smart field if you really want to use your own reference.

Let me know if that helps

1 Like

Of course, I’ve done that. And it’s not working between those 2 collections. I ignore why.

Could you please provide the necessary requirement from the issue template (Project name, packages versions …), so I can check the behavior on my side using a similar configuration ?

"name": "lereacteur-admin",
"dependencies": {
    "express": "~4.16.3",
    "forest-express-mongoose": "^6.3.5",
    "mongoose": "~5.8.2",
  },

Is that enough for you ?

I’ve just tested it again with a new mongo database and running forest-express-mongoose: 6.3.5 using a referenced field (Directly on collection & a smart field), and I’m still unable to reproduce your issue…

Just to be sure I’m testing it the exact same way you did:

Because it’s a mongoose reference

You mean an ObjectId/_id reference here ?

Normally, when you start typing in the dropdown/search field, an HTTP request on the collection shoud be triggered. Is it possible for you to send me the query parameters sent within that request?
Also, did you try switching to a belongsTo widget instead of the search dropdown one?

Thanks in advance

Yes.

Here is the HTTP request :
http://localhost:3310/forest/Fundings?context[record][type]=incoming&context[record][id]=5f3f5f30ebab6c0017efa56f&fields[Fundings]=Référence&page[number]=1&page[size]=100&search=arine&searchToEdit=true&timezone=Europe%2FParis

I should find “Marine” but I’ve a “No results found”.

My custom field is :

{
      field: "Référence",
      type: "String",
      get: async (funding) => {
        const fundingObj = await Fundings.findById(funding._id)
          .populate("lead");
        const { lead } = fundingObj;
        return lead.firstname;
      },

If I use belongsTo it will list a tons of data and it’s doesn’t fit my needs. I need an autocomplete, that why I use a dropdown.

I asked for the belongsTo just to know if that works as expected (If you are viewing the firstname in it instead of an id).

The Référence part in the URL seems weird to me (accent should be encoded, but that seems to be an issue from our side), but I still did a quick test on my end (Renaming my field to Référence) with no issue related to that point. I would still suggest to try renaming it to “Reference” just to see if that helps.

Also, fields[Fundings] does not looks right to me. I’ll dig into this, but I’m pretty sure collection name should be generated in lower case. Did you change anything related to that part ?

Yes it works.

I also tried with and without accent, lower/uppercase and it’s the same.

No, upper/lowercase should be ok. I didn’t change anything about that part.

Just as a quick explanation, you shouldn’t need a smart field to achieve what you are trying to do. dropdown widget for belongsTo relation is able to retrieve datas using the display name you set on the collection, so there might be another problem around the model definition.

Could you please provide the models definitions for the two classes you are trying to link ?

Also, if that’s possible on your side, I would suggest to try to use the referenceName you want to search before trying to achieve it with the smart field, since that would be easier to locate what is happening.

Here is the model Fundings that has a “Référence” field :

const schema = mongoose.Schema(
  {
    funder: { type: mongoose.Schema.Types.ObjectId, ref: "Funders" },
    lead: { type: mongoose.Schema.Types.ObjectId, ref: "Leads" },
    paymentMethod: String,
    amount: Number,
    chargeDate: Date,
    createdAt: {
      type: Date,
      default: Date.now,
    },
  },
  {
    timestamps: false,
  },
);

module.exports = mongoose.model("Fundings", schema, "fundings");

And the model Incomings :

const schema = mongoose.Schema(
  {
    funding: { type: mongoose.Schema.Types.ObjectId, ref: "Fundings" },
    transaction_id: String,
    amount: Number,
    local_amount: Number,
    currency: String,
    local_currency: String,
    label: String,
    settled_at: Date,
    emitted_at: Date,
    updated_at: Date,
    note: String,
    reference: String,
    chargeDate: Date,
  },
  {
    timestamps: false,
  },
);

module.exports = mongoose.model("Incomings", schema, "incomings");

As you can see on the Fundings model, there is no name. I must populate the collection in order to get a firstname and lastname for instance. That’s why I need a smart field. But anyway, even if I chose the field amount as the reference it’s not working as expected.

I’m running a very similar example as yours, but still, I’m not able to reproduce this issue.

If you don’t have problem with this, maybe a .zip of your code would help to reproduce. If it is not, could you provide the Leads model definition?

Also, do you have some kind of warning logs when you start your lumber server ?