Mongoose“属性x不存在于类型y”错误

编程入门 行业动态 更新时间:2024-10-25 00:26:43
Mongoose“属性x不存在于类型y”错误 - 仍然有效(Mongoose “property x does not exist on type y” error - still works)

使用Mongoose,我得到error TS2339: Property 'highTemp' does not exist on type 'Location'虽然代码仍然按预期工作,但尝试使用点符号( model.attribute )时, error TS2339: Property 'highTemp' does not exist on type 'Location' 。 在这里的评论中,我了解到使用model['attribute']不会产生任何错误。

什么是正确的方式能够使用点符号与Mongoose没有错误?

背景:

location.model.ts

import mongoose = require('mongoose'); export const LocationSchema = new mongoose.Schema({ name: String, lowTemp: Number, highTemp: Number, }); export const Location = mongoose.model('Location', LocationSchema);

data.util.ts

import { Location } from '../models/location.model'; function temperatureModel(location: Location): number { const highTemp = location.highTemp; const lowTemp = location['lowTemp']; // Do the math... return something; }

建立上面的结果会产生highTemp上的TS2339错误,但不会产生lowTemp上的错误。 我使用模型属性的首选方法是使用点记号,如在location.highTemp 。 我该怎么办? 明确定义每个模型的接口听起来像没有意义的工作..?

With Mongoose, I get error TS2339: Property 'highTemp' does not exist on type 'Location' when trying to use dot notation (model.attribute) although the code still works as intended. In the comments here I learned that using model['attribute'] yields no error.

What is the proper way to be able to use dot notation with Mongoose without errors?

Background:

location.model.ts

import mongoose = require('mongoose'); export const LocationSchema = new mongoose.Schema({ name: String, lowTemp: Number, highTemp: Number, }); export const Location = mongoose.model('Location', LocationSchema);

data.util.ts

import { Location } from '../models/location.model'; function temperatureModel(location: Location): number { const highTemp = location.highTemp; const lowTemp = location['lowTemp']; // Do the math... return something; }

Building the above yields the TS2339 error on highTemp but not on lowTemp. My preferred method of using model attributes would be with dot notation as in location.highTemp. What should I do? Explicitly defining interfaces for every model sounds like pointless work..?

最满意答案

model方法接受一个接口(需要扩展Document ),它可以用来静态输入结果:

export interface Location extends mongoose.Document { name: string, lowTemp: number, highTemp: number, } export const Location = mongoose.model<Location>('Location', LocationSchema); // Usage function temperatureModel(location: Location): number { const highTemp = location.highTemp; // Works const lowTemp = location.lowTemp; // Works }

The model method accepts an interface (which needs to extends Document) which can be used to statically type the result:

export interface Location extends mongoose.Document { name: string, lowTemp: number, highTemp: number, } export const Location = mongoose.model<Location>('Location', LocationSchema); // Usage function temperatureModel(location: Location): number { const highTemp = location.highTemp; // Works const lowTemp = location.lowTemp; // Works }

更多推荐

本文发布于:2023-08-07 11:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   属性   错误   类型   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!