离子-运行时错误:Object(...)不是函数

时间:2018-06-27 06:08:52

标签: node.js angular typescript ionic-framework ionic3

我有一个非常基本的项目:

https://github.com/napolev/ionic-start

基本上是Ionic入门项目:

// https://ionicframework.com/getting-started
$ ionic start ionic-start tabs

我添加了以下两个文件:

/src/extras/social-media.ts

import * as Promise from 'bluebird';
import { User } from './user';

export var LoginPromise = function(service): Promise<string> {
    return new Promise((resolve) => {
        setTimeout(function(){
            resolve('Will Smith');
        }, 2000)
    });
};

//*/ IF I COMMENT THIS BLOCK OUT, THERE IS NO ERROR
// this function is not used by this project
// but for some specific reasons it is required to be here
// this code is a simplification of a bigger project
export var callMeIfYouNeedMe = function(id) {
    return new Promise((resolve) => {
        User.findById(id, function(err, user) {
            console.log(user);
            resolve(user);
        });
    });
}
// END OF BLOCK */

/src/extras/user.ts

import { Document, Schema, Model, model} from "mongoose";

export interface IUser {
    name: string;
}
export interface IUserDocument extends IUser, Document {

}
export var UserSchema: Schema = new Schema({
    name: String,
});

export const User: Model<IUserDocument> = model<IUserDocument>("User", UserSchema);
  

上面的代码在节点项目上可以正常工作(我自己尝试过)。我是从http://brianflove.com/2016/10/04/typescript-declaring-mongoose-schema-model/#schema--model获得的   在上一次导出时,我创建了一个类型为IUserDocument的模型。

我还修改了以下两个文件:

/src/pages/home/home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LoginPromise } from '../../extras/social-media';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {

  }

  test() {
    console.log('Hello World');
    LoginPromise('facebook').then(
      (name: string) => {
        console.log('### Social Media -> ' + name);
      }
    );
  }

}

/src/pages/home/home.html

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h2>Welcome to Ionic!</h2>
  <p>
    This starter project comes with simple tabs-based layout for apps
    that are going to primarily use a Tabbed UI.
  </p>
  <p>
    Take a look at the <code>src/pages/</code> directory to add or change tabs,
    update any existing page or create new pages.
  </p>

  <button ion-button (click)="test()" color="primary">Test</button>

</ion-content>

我的问题是,当我跑步时:

$ ionic serve --no-open

我在控制台上没有收到任何错误,但是在浏览器上我得到了以下信息:

"Runtime Error: Object(...) is not a function"

如您所见:

enter image description here

您对这里可能发生的事情有什么想法,我怎么做?

谢谢!

1 个答案:

答案 0 :(得分:0)

  

运行时错误:Object(…)不是函数

您的通话:

model<IUserDocument>("User", UserSchema);

是错误的。 model不是函数。它是一个对象。

修复

调用一个对象。代码错误。