【问题标题】:Typescript - NodeJS - Mongoose打字稿-NodeJS-猫鼬
【发布时间】:2016-02-05 13:04:00
【问题描述】:

我刚刚开始使用 typescript 进行 NodeJS 服务器开发,我遇到了以下错误:

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

我已经尝试了很长一段时间,但我无法摆脱错误。

代码本身按预期运行,但我不断收到这些错误。

感谢任何形式的帮助。

【问题讨论】:

    标签: node.js mongoose typescript


    【解决方案1】:

    我在使用来自definitelyTyped github repo 的 mongoose.d.ts 时遇到了类似的问题。似乎它没有正确匹配 mongoose javascript。我改用这里的https://github.com/vagarenko/mongoose-typescript-definitions

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 2020-05-11
      • 2016-04-01
      • 1970-01-01
      • 2017-04-11
      • 2017-01-20
      • 2021-01-05
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多