gpt4 book ai didi

javascript - 如何在 javascript 中克隆 mongoose 查询对象

转载 作者:搜寻专家 更新时间:2023-10-31 23:29:59 25 4
gpt4 key购买 nike

我面临着 mongoose query 的克隆问题目的 。 Javascript通过 call-by-ref 将一个对象复制到另一个对象中但在我的项目中有一种情况我需要通过 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 and query1 object is used for count query.

1.我以前用Object.clone(query1)报错Object.clone is not function 2.我使用了 Object.assign(query1) 但它不能正常工作。 3.我用了很多其他方法谁能帮我解决这个问题

最佳答案

使用 merge 方法的替代解决方案:

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()])

关于javascript - 如何在 javascript 中克隆 mongoose 查询对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36759997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com