I am facing the problem of clone of the mongoose query
object .Javascript
the copy the one object into another object by call-by-ref
but in my project there is scenario i need to copy one object into another object by call-by-value.
var query=domain.User.find({
deleted: false,
role: role
})
var query1=query;
I have the scenario change in the query object is not reflected in
query1
. I google and try so many way to clone the object but it does't work.The query object is used in another function for pagination andquery1
object is used for count query.
1.I used to Object.clone(query1) error Object.clone is not function 2.I used Object.assign(query1) but it does't works fine. 3.I used other so many ways can anybody help me to sort this problem
you are trying to clone a cursor, but it is not the right approach, you probably just need to create another
like this:
var buildQuery = function() {
return domain.User.find({
deleted: false,
role: role
});
};
var query = buildQuery();
var query1 = buildQuery();
Alternative solution using merge
method:
const query = domain.User.find({
deleted: false,
role: role
}).skip(10).limit(10)
const countQuery = query.model.find().merge(query).skip(0).limit(0)
const [users, count] = await Promise.all([query, countQuery.count()])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With