简体   繁体   English

打字稿-NodeJS-猫鼬

[英]Typescript - NodeJS - Mongoose

I've just started using typescript for NodeJS Server development and I've run into the following errors: 我刚开始使用Typescript进行NodeJS Server开发,但遇到以下错误:

./app/api/shoutbox/shoutbox.dao.ts
error TS2339: Property 'statics' does not exist on type 'Schema'

./app/api/shoutbox/shoutbox.controller.ts
error TS2339: Property 'getAll' does not exist on type 'Model<Document>'.

./app/api/shoutbox/shoutbox.controller.ts
error TS2339: Property 'catch' does not exist on type 'Promise<{}>'.

I am using Webpack and TS-Loader. 我正在使用Webpack和TS-Loader。 And I've referenced 我已经引用

/// <reference path="mongoose/mongoose.d.ts" />
/// <reference path="node/node.d.ts" />
/// <reference path="express/express.d.ts" />
/// <reference path="mime/mime.d.ts" />
/// <reference path="serve-static/serve-static.d.ts" />
/// <reference path="lodash/lodash.d.ts" />
/// <reference path="bluebird/bluebird.d.ts" />



// shoutbox.model.ts
import mongoose = require('mongoose'); 
var _shoutboxSchema = {
    author: {
        type: String,
        required: true
    },
    created: {
        type: Date,
        default: Date.now
     },
     msg: String
};
export default new mongoose.Schema(_shoutboxSchema);


// shoutbox.dao.ts
import mongoose = require('mongoose');
import Promise = require('bluebird');
import _ = require('lodash');
import shoutboxSchema from './shoutbox.model';

shoutboxSchema.statics.getAll = ():Promise<any> => {
    var _promise = (resolve:Function, reject:Function):void => {
        var _query = {};
        Shoutbox
             .find(_query)
             .exec((err, shoutbox) => {
                err ? reject(err)
                    : resolve(shoutbox);
             });
    };
    return new Promise (_promise);
};
var Shoutbox = mongoose.model('Shoutbox', shoutboxSchema);
export default Shoutbox;

// shoutbox.controller.ts
import ShoutboxDAO from './shoutbox.dao';
import express = require("express");

export class ShoutboxController {
    static  getAll(req:express.Request, res:express.Response):void {
         ShoutboxDAO
            .getAll()
            .then(shoutbox => res.status(200).json(shoutbox))
            .catch(error => res.status(400).json(error));
    }
}

I've tried around for quite some time now but i cant get rid of the errors. 我已经尝试了一段时间,但我无法摆脱错误。

The code itself runs as intended but i keep getting these errors. 代码本身按预期运行,但我不断收到这些错误。

Im thankful for any sort of help. 我感谢任何帮助。

I've encountered similar problems with using the mongoose.d.ts from DefinitelyTyped github repo. 我在使用DefinitelyTyped github存储库中的mongoose.d.ts时遇到了类似的问题。 It seems it isn't correctly matching the mongoose javascript. 似乎与猫鼬javascript不正确匹配。 I instead use the one from here https://github.com/vagarenko/mongoose-typescript-definitions . 我改为使用https://github.com/vagarenko/mongoose-typescript-definitions中的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM