【问题标题】:Typescript error in Mongodb update query operatorsMongodb更新查询运算符中的打字稿错误
【发布时间】:2021-11-16 18:56:12
【问题描述】:

我是 Mongodb 的新手,当我尝试使用 push 方法将对象数组推送到 mongodb 时遇到打字错误。为此,我正在使用包装器 mongdb 服务。

public saveRental = () => {
    return this.connector
      .connect()
      .then(() => {
        return this.connector.update(
          "polcyData", // collection name
          { _id: "dummyid" },
          { "$push": { "myArray": { "field1": "abc", "field2": "def" } } },
        );
      });
};

这是我在悬停 $push 方法时遇到的错误:

(property) $push?: PushOperator<any> | undefined
Type '{ myArray: { field1: string; field2: string; }; }' is not assignable to type 'PushOperator<any>'.
  Type '{ myArray: { field1: string; field2: string; }; }' is not assignable to type 'NotAcceptedFields<any, any[]>'.
    Property 'myArray' is incompatible with index signature.
      Type '{ field1: string; field2: string; }' is not assignable to type 'undefined'.ts(2322)
index.d.ts(1256, 5): The expected type comes from property '$push' which is declared here on type 'UpdateQuery<any>'

这是更新方法参数:

update(collectionName: string, filter: MongoClient.FilterQuery<any>, update: MongoClient.UpdateQuery<any>, options?: MongoClient.UpdateOneOptions): Promise<any>;

这是 UpdateQuery 类型

/** https://docs.mongodb.com/manual/reference/operator/update */
export type UpdateQuery<TSchema> = {
    /** https://docs.mongodb.com/manual/reference/operator/update-field/ */
    $currentDate?: OnlyFieldsOfType<TSchema, Date, true | { $type: 'date' | 'timestamp' }>;
    $inc?: OnlyFieldsOfType<TSchema, number>;
    $min?: MatchKeysAndValues<TSchema>;
    $max?: MatchKeysAndValues<TSchema>;
    $mul?: OnlyFieldsOfType<TSchema, number>;
    $rename?: { [key: string]: string };
    $set?: MatchKeysAndValues<TSchema>;
    $setOnInsert?: MatchKeysAndValues<TSchema>;
    $unset?: OnlyFieldsOfType<TSchema, any, ''>;

    /** https://docs.mongodb.com/manual/reference/operator/update-array/ */
    $addToSet?: SetFields<TSchema>;
    $pop?: OnlyFieldsOfType<TSchema, any[], 1 | -1>;
    $pull?: PullOperator<TSchema>;
    $push?: PushOperator<TSchema>;
    $pullAll?: PullAllOperator<TSchema>;

    /** https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
    $bit?: {
        [key: string]: { [key in 'and' | 'or' | 'xor']?: number };
    };
};

谁能帮我解决这个问题?任何帮助将非常感激。该错误仅适用于更新数组运算符: https://docs.mongodb.com/manual/reference/operator/update-array

【问题讨论】:

  • 你试过了吗:{ "myArray": { "$push": { "field1": "abc", "field2": "def" } } },
  • @matthPen 是,但仍然出现错误:参数类型 '{ myArray: { $push: { field1: string;字段2:字符串; }; }; }' 不可分配给“UpdateQuery”类型的参数。对象字面量只能指定已知属性,并且 'myArray' 不存在于类型 'UpdateQuery' 中。
  • {$set:{ "myArray": { "$push": { "field1": "abc", "field2": "def" } } }} ?
  • @matthPen 这次打字稿错误,但我无法保存它的抛出错误:“'myArray.$push' 中的美元 ($) 前缀字段 '$push' 对存储。”

标签: javascript node.js mongodb typescript mongodb-query


【解决方案1】:

如果您遇到同样的问题,请检查您的节点版本。

我遇到这个问题是因为我当前的本地节点版本不是package.json中设置的版本

检查您是否在package.json 中指定了节点版本

"engines": {
    "node": "^15.7.0"
}

删除node_modulespackage-lock.json

rm -Rf node_modules package-lock.json

设置正确的节点版本

echo "v15.7.0" > .nvmrc
nvm install
nvm use

最后重新安装包

npm install

【讨论】:

    猜你喜欢
    • 2022-09-30
    • 2018-12-30
    • 2020-11-05
    • 2016-02-25
    • 2021-07-23
    • 2018-09-07
    • 1970-01-01
    • 2015-01-01
    • 2020-12-13
    相关资源
    最近更新 更多