首页 > 解决方案 > 离子 - 运行时错误:对象(...)不是一个函数

问题描述

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

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"

正如你在这里看到的:

在此处输入图像描述

您对这里可能发生的事情以及如何使这项工作有任何想法吗?

谢谢!

标签: node.jsangulartypescriptionic-frameworkionic3

解决方案


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

你的来电 :

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

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

使固定

不要调用对象。代码是错误的。


推荐阅读