NodeJS + Express + MongoDB使用Mongoose序列不工作

ia2d9nvy  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(100)

a尝试在_id数据中使用自动递增数字来创建集合,但保持“发送请求”,并且在此调用中没有回调。我真的不知道我不能做什么工作

"mongoose": "^7.5.3"
"mongoose-sequence": "^5.3.1"
"node": "v20.8.0"
  • domain.model.js*
const mongoose = require("mongoose");
const AutoIncrement = require("mongoose-sequence")(mongoose);

const schema = mongoose.Schema(
  {
    _id: {
      type: Number,
    },

    domain: {
      type: String,
      required: true,
      trim: true,
    },
  }
);
schema.plugin(AutoIncrement, { inc_field: "_id" });
const domain = mongoose.model("domain", schema);

module.exports = domain;
  • 我的职责 *
const isSetSearchDomain = async (dom) => {
  try {
    let promise = new Promise(async (resolve) => {
      const domainPromises = dom.map(async (domain) => {
        let existingDomain = await Domain.findOne({ domain: domain });

        if (!existingDomain) {
          const newDomain = await new Domain({
            domain: domain
          });

          await newDomain
            .save()
            .then((documento) => {
              console.log("Documento salvo com sucesso:", documento);
            })
            .catch((err) => {
              console.error(err);
            });

          await newDomain.setNext("_id", function (err, dom) {
            console.log(dom._id);
          });

          console.log(newDomain);

          return {
            _id: newDomain._id,
            domain: domain,
          };
        } else {
          return {
            _id: existingDomain._id,
            domain: domain,
          };
        }
      });

      const results = await Promise.all(domainPromises);
      resolve(results);
    });

    return promise.then((p) => p);
  } catch (error) {
    console.log(error);
    log.error(error);
    return false;
  }
};

当我把await放在await newDomain.save()中时,仍然在加载请求
它一直在加载一个API请求,但没有返回任何东西,我真的不知道错误到底是什么,我已经尝试了所有方法来使用增量id进行收集

ckx4rj1h

ckx4rj1h1#

Remove _id from Schema as您不需要在模式中定义id字段,因为插件会自动为您设置它。你唯一要做的就是打电话。

相关问题