gpt4 book ai didi

mongoose - TS2351 : Cannot use 'new' with an expression whose type lacks a call or construct signature. Mongoose

转载 作者:行者123 更新时间:2023-12-02 19:22:01 27 4
gpt4 key购买 nike

不确定我做错了什么,但我有这个类/模块:

import * as Mongoose from 'mongoose';
import * as _ from 'lodash';
import * as Promise from 'bluebird';
import { db } from '../lib/database';

let mongoose = Mongoose;

mongoose.Promise = Promise;


export class BaseModel {

constructor(schemaObj: {}, modelName: string, statics?: {}) {
let Schema = new mongoose.Schema(schemaObj)

Schema.statics.list = function () {
return this.find()
}

Schema.statics.save = function (dataObj) {
return this.save(dataObj)
}

if (statics) {
for (let key in statics) {
Schema.statics[key] = statics[key];
}
}

return mongoose.model(modelName, Schema);
}

}

let schema = {
title: String,
}

let Message = new BaseModel(schema, 'message');

let j = new Message({title: 'Hello World'}).save().then(function (res) {
console.log(res);
})

一切实际上都有效,但 TypeScript 正在提示:

TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

我知道我错过了一些东西,但我不知道是什么。

最佳答案

尝试在导入 mongoose 时删除 * as。所以它会变成
从 'mongoose' 导入 Mongoose;

关于mongoose - TS2351 : Cannot use 'new' with an expression whose type lacks a call or construct signature. Mongoose ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com