简体   繁体   English

执行 promise 或等待生成的字符串变量

[英]Execute promise or await with generated string variable

I am building a mongoose query and storing it in a variable call query .我正在构建一个 mongoose 查询并将其存储在变量调用query中。 The code below shows it下面的代码显示了它

let query = "Product.find(match)";
if (requestObject.query.sortBy) {
    query = query.concat(".", "sort(sort)");
    const parts = requestObject.query.sortBy.split(":");
    sort[parts[0]] = parts[1] === "desc" ? -1 : 1;
  }
  if (requestObject.query.fields) {
    query = query.concat(".", "select(fields)");
    const fields = requestObject.query.fields.split(",").join(" ");
    const items = await Product.find(match).sort(sort).select(fields); //.populate("category").exec();
    /**const items = await Product.find(match).sort(sort).select("-__v"); //.populate("category").exec();**/
  }

I am facing an issue when attempting to run a mongoose query that I have generated and stored in a string.我在尝试运行我生成并存储在字符串中的 mongoose 查询时遇到问题。 When I run it in post man, the response is 200 but no data is returned.当我在 post man 中运行它时,响应为 200 但没有返回任何数据。 Below is a console.log(query) on line 2下面是第 2 行的console.log(query)

该图显示了第 2 行查询控制台的日志

what I hope to achieve is to have await or create a new promise execute the content id query variable like shown below我希望实现的是等待或创建一个新的 promise 执行内容 ID 查询变量,如下所示

const items = new Promise((resolve) => resolve(query)); //.populate("category").exec();
  items
    ? responseObject.status(200).json(items)
    : responseObject
        .status(400)
        .json({ message: "Could not find products, please try again" });

I will appreciate it very much that and also if you can give me a better way of doing it, I will love that我会非常感激,如果你能给我一个更好的方法,我会喜欢的

This doesn't really make sense.这真的没有意义。 You are building a string, not a query.您正在构建一个字符串,而不是一个查询。 You can't do anything with that string.你不能用那个字符串做任何事情。 (You could eval it, but you really shouldn't). (你可以eval它,但你真的不应该这样做)。 Instead, build a query object!相反,构建一个查询对象!

let query = Product.find(match);
if (requestObject.query.sortBy) {
  const [field, dir] = requestObject.query.sortBy.split(":");
  const sort = {};
  sort[field] = dir === "desc" ? -1 : 1;
  query = query.sort(sort);
}
if (requestObject.query.fields) {
  const fields = requestObject.query.fields.split(",");
  query = query.select(fields);
}

//query.populate("category")

const items = await query.exec();
if (items) {
  responseObject.status(200).json(items)
} else {
  responseObject.status(400).json({ message: "Could not find products, please try again" });
}

If you really want to get that string for something (eg debugging), build it separately from the query:如果您真的想为某些事情(例如调试)获取该字符串,请将其与查询分开构建:

let query = Product.find(match);
let queryStr = 'Product.find(match)';
if (requestObject.query.sortBy) {
  const [field, dir] = requestObject.query.sortBy.split(":");
  const sort = {[field]: dir === "desc" ? -1 : 1};
  query = query.sort(sort);
  queryStr += `.sort(${JSON.stringify(sort)})`;
}
if (requestObject.query.fields) {
  const fields = requestObject.query.fields.split(",");
  query = query.select(fields);
  queryStr += `.select(${JSON.stringify(fields)})`;
}

//query.populate("category")
//queryStr += `.populate("category")`;

console.log(queryStr);
const items = await query.exec();
…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM