Error query Mongoose

Asked

Viewed 102 times

-1

I have an appointment at Nodejs with Mongodb:

filter:['abertos','cancelados'];

schemas.chamados
    .find({STATUS_CHAMADO : {"$in" : filter}})
    .exec(function(err, dados) {
        if(err) return callback(err);
        else {
            return callback(dados);
        }
    });

Displays the following error:

Object {message: "Cast to string failed for value "undefined" at path "STATUS_CHAMADO"", name: "CastError", type: "string", path: "STATUS_CHAMADO"}

Does anyone know what it can be ?

When the user selects the filter on the angular front end, the Node will search for these values.

  • This filter comes from a checkbox passed by the angular, If it comes like this: ["open", null, null], the error happens now if it comes ["open", "canceled", "finished"] will normally.

  • Yes is constant, even using a string does not work! So of the error if the filter variable comes from the angle, if I set it manually works.

  • The same mistake happens !!

  • even if it is {'status_chamado' : { '$in': filter}} appears "Cast to string failed for value "status_chamado" at path "status_chamado"" Then you wouldn’t need to cast if it’s already string

  • Yes, even then the error appears, but only when the filter is variable. If I set var filter=['open','cleared','finished'] not from the error.

  • (I removed the previous comments) Maybe this filter isn’t coming as an array, try to validate by putting something like console.log(typeof filter)

  • Testei, returns as Object array

  • I’ve tried everything to my knowledge, someone has some idea that it might be happening ?

  • So that’s it. It should be an array. Convert to array that will solve

  • Caputo, solved. You were right ! Thank you very much.

Show 5 more comments

1 answer

1

As @Caputo said, I just converted object in array as below:

var arr = Object.keys(filter).map(function(k) { return filter[k] });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.