【问题标题】:Why do I get array of nulls in my database? [duplicate]为什么我的数据库中会出现空数组? [复制]
【发布时间】:2019-04-26 03:13:48
【问题描述】:

我有一个 ID 数组,即 launchIds。 我正在尝试将其推送到模型字段trips $addToSet: { trips: { $each: launchIds }。这给了我一个错误:Cast to [ObjectId] failed for value \"[\"1\",\"2\",\"3\"]\...

如果我尝试通过launchIds 映射并转换为Mongoose.Shema.Types.ObjectId,我会进入数据库trips: [null,null,null]

lauchIds = ['1','2','3']

  async bookTrips({ launchIds }) {
    let userId = "5bf7f7b3817119363da48403";

    const mongoIds = launchIds.map(l => Mongoose.Schema.Types.ObjectId(l));

    return this.store.User.findByIdAndUpdate(
      { _id: userId },
      {
        $addToSet: { trips: { $each: mongoIds } }
      },
      { new: true }
    );
  }

这是我的模型架构:

  const UserSchema = new Mongoose.Schema(
    {
      email: {
        type: String,
        required: true
      },
      token: String,
      trips: [
        {
          type: Mongoose.Schema.Types.ObjectId,
          ref: "trip"
        }
      ]
    },
    { timestamps: true }
  );

我正在通过 grapql 游乐场传递 ID。这是我的mutation

bookTrips: async (_, { launchIds }, { dataSources }) => {
  console.log(launchIds);
  // logs ['1','2','3']
  console.log(typeof launchIds);
  //Object 

  const results = await dataSources.userAPI.bookTrips({ launchIds });
  console.log(results);

  return { message: "hello" };
}

【问题讨论】:

  • 看起来 launchIds 是某种转义字符串。您能告诉我们您实际设置该变量的位置吗? (即你怎么打电话给booktrips
  • 更新了帖子。这更有意义吗?
  • 我们能看到控制台,还有console.log(typeof launchIds);?
  • 嗯...它是一个对象

标签: mongodb mongoose


【解决方案1】:

我得到了一个字符串数组,其中应该是数字

解决办法:

我的模型(同上):

  const UserSchema = new Mongoose.Schema(
    {
      email: {
        type: String,
        required: true
      },
      token: String,
      trips: [
        {
          type: Mongoose.Schema.Types.ObjectId,
          ref: "trip"
        }
      ]
    },
    { timestamps: true }
  );

粗略 API:

  async bookTrips({ launchIds }) {
    let userId = "5bf7f7b3817119363da48403";

    const idsToNums = launchIds.map(Number);
    const mongoIds = idsToNums.map(l => Mongoose.Types.ObjectId(l));

    return this.store.User.findByIdAndUpdate(
      { _id: userId },
      {
        $push: { trips: { $each: mongoIds } }
      },
      { new: true }
    );
  }

请注意模型上的 Mongoose.Schema.Types.ObjectId 和 api 上的 Mongoose.Types.ObjectId。如果我从模型中删除 Schema 或将 Schema 添加到 api 我会收到错误消息。不知道为什么,但上面的例子有效。我希望有人会发现这很有帮助或提出更好的解决方案。

【讨论】:

【解决方案2】:

要将字符串或数字转换为 mongo 对象,请使用Mongoose.Types.ObjectId

const mongoIds = launchIds.map(l => Mongoose.Types.ObjectId(l));

【讨论】:

  • 我收到一个错误:"Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
  • Mongoose.Types.ObjectId(1) => 000000012a5f832b959ac1a4。我没有收到任何错误。
  • 更改 Mongoose.Schema.Types.ObjectId => Mongoose.Types.ObjectId
  • 在控制台中给我一个错误:node_modules/mongoose/lib/schema.js:681 throw new TypeError('Undefined type ' + name + '` at array ' + path + ^ TypeError: Undefined type ObjectID` at array `trips``
  • 你可以检查这个repl.it/@rajasekarm/Stackoverflow-mongoose-issue-fix,点击运行查看输出
猜你喜欢
  • 2021-09-18
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多