簡體   English   中英

打字稿-NodeJS-貓鼬

[英]Typescript - NodeJS - Mongoose

我剛開始使用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<{}>'.

我正在使用Webpack和TS-Loader。 我已經引用

/// <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));
    }
}

我已經嘗試了一段時間,但我無法擺脫錯誤。

代碼本身按預期運行,但我不斷收到這些錯誤。

我感謝任何幫助。

我在使用DefinitelyTyped github存儲庫中的mongoose.d.ts時遇到了類似的問題。 似乎與貓鼬javascript不正確匹配。 我改為使用https://github.com/vagarenko/mongoose-typescript-definitions中的一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM