node.js - 离子 - 运行时错误:对象(...)不是一个函数
问题描述
我有这个非常基本的项目:
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"
正如你在这里看到的:
您对这里可能发生的事情以及如何使这项工作有任何想法吗?
谢谢!
解决方案
运行时错误:Object(...) 不是函数
你的来电 :
model<IUserDocument>("User", UserSchema);
是错的。model
不是函数。它是一个对象。
使固定
不要调用对象。代码是错误的。
推荐阅读
- github-pages - 更新后无法提供雨果主题;获取“调用部分错误:模板执行失败:调用定界错误:无法迭代
" - css - 如何修复角材料可拖动项目
- spring - Firebase Admin SDK - 令牌尚未生效
- javascript - Flowable runtimeService.startProcessInstanceByKey('MY_SUBPROCESS_KEY') 执行但 superProcessId null
- c# - 如何导致方法调用先进入不同的方法
- amazon-s3 - 如何在 bluidspec.uml 中指定写入 s3 存储桶?
- django - Django Rest Framework - 为非内置用户模型类生成令牌
- java - Java:从给定的字符串中提取数字
- c# - 与基类和泛型类型匹配的 C# 模式
- git - gitlab 不克隆( ssh )停留在“克隆到'repo'......”