node.js - Ionic - 运行时错误 : Object(. ..) 不是函数

标签 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);

The code right above works properly on a node project (I tried by myself). I got it from: http://brianflove.com/2016/10/04/typescript-declaring-mongoose-schema-model/#schema--model With the last export I create a model of type: 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

您知道这里会发生什么以及我如何才能完成这项工作吗?

谢谢!

最佳答案

Runtime Error: Object(…) is not a function

您的电话:

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

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

修复

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

关于node.js - Ionic - 运行时错误 : Object(. ..) 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055635/

相关文章:

javascript - 仅使用 HTML 和 CSS 进行语法高亮显示

node.js - 如何在heroku上使用react路由?

javascript - 检测 Node.js 中的硬链接(hard link)

node.js - Node/Express : EADDRINUSE, 地址已在使用 - 终止服务器

javascript - ngrx/store 与 ngModel 引起了一些问题

javascript - 确定 javascript 对象上的所有属性是否为 null 或空字符串

javascript - 使用 node.js 进行正确的 PostgreSQL 查询

javascript - 在 .js 文件中获取 Angular 环境

Angular 6 - 错误类型错误 : "this.router is undefined"

angular - 如何修复我的 Angular 测验应用程序中多选题的评分问题?