Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何从mongoose获取类型化对象_Node.js_Mongodb_Typescript_Mongoose_Mongoose Schema - Fatal编程技术网

Node.js 如何从mongoose获取类型化对象

Node.js 如何从mongoose获取类型化对象,node.js,mongodb,typescript,mongoose,mongoose-schema,Node.js,Mongodb,Typescript,Mongoose,Mongoose Schema,我在typescript中有一个nodejs应用程序,最近我开始使用mongoose查询mongodb,但我正在努力找到一种从findById方法获取类型化对象的方法。有没有办法通过某种方式从该方法获取类型化对象 import * as mongoose from 'mongoose'; export const LinksCacheSchema = new Schema({ ts: { type: Date, default: Date.now()

我在typescript中有一个nodejs应用程序,最近我开始使用mongoose查询mongodb,但我正在努力找到一种从findById方法获取类型化对象的方法。有没有办法通过某种方式从该方法获取类型化对象

import * as mongoose from 'mongoose';
export const LinksCacheSchema = new Schema({
    ts: {
        type: Date,
        default: Date.now()
    },
    imdbId: String,
    parentLink: String,
    playableLink: String,
    status: String,
    title: String,
    size: Number,
    contentType: String,
}, { collection: 'links_cache' });

export const LinksCacheList = mongoose.model('LinksCache', LinksCacheSchema);


const linkInfo = await LinksCacheList.findById(documentId); //this is the one returning type of any instead of LinksCacheSchema or any specific type

您可以为该架构创建、导出和接口:

import mongoose, { Schema, Document, Model } from 'mongoose';

export interface ILinksCacheSchema extends Document {
    ts: Date,
    imdbId: string,
    parentLink: string,
    playableLink: string,
    status: string,
    title: string,
    size: number,
    contentType: string,
}

const LinksCacheSchema = new Schema({
    ts: {
        type: Date,
        default: Date.now()
    },
    imdbId: String,
    parentLink: String,
    playableLink: String,
    status: String,
    title: String,
    size: Number,
    contentType: String,
}, { collection: 'links_cache' });

export const LinksCacheList: Model<ILinksCacheSchema> = mongoose.model('LinksCache', LinksCacheSchema);

// in your app logic
const linkInfo = await LinksCacheList.findById(documentId);
从“mongoose”导入mongoose,{Schema,Document,Model};
导出接口ILinkCacheSchema扩展文档{
ts:日期,
imdbId:string,
parentLink:string,
可播放链接:字符串,
状态:字符串,
标题:字符串,
尺寸:数量,
contentType:string,
}
const LinksCacheSchema=新模式({
ts:{
类型:日期,
默认值:Date.now()
},
imdbId:String,
parentLink:String,
可播放链接:字符串,
状态:字符串,
标题:字符串,
尺寸:数量,
contentType:String,
},{集合:'links_cache'});
export const LinksCacheList:Model=mongoose.Model('LinksCache',LinksCacheSchema);
//在应用程序逻辑中
const linkInfo=wait LinksCacheList.findById(documentId);
创建两次模式可能会变得有点乏味,因此我建议您看看以下内容: