- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 TypeScript 与 Express 和 Typeorm 创建一个项目。
在使用 tsc
编译后尝试使用 node dist/index.js
运行服务器时,我不断收到以下错误。
> typescript-express-boilerplate@1.0.0 start /home/yusta/Desktop/project
> npm run serve
> typescript-express-boilerplate@1.0.0 serve /home/yusta/Desktop/project
> node dist/index.js
Example index listening on port 3000!
/home/yusta/Desktop/project/src/entity/User.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
有趣的是,运行 ts-node src/index.ts
工作正常:
Example index listening on port 3000!
Saved a new user with id: 2
Loading users from the database...
Loaded users: [ User { id: 2, email: 'hello@hello.com', password: 'password' } ]
我认为这是 tsconfig.json 的问题,但我已经将 module
设置为 commonjs
,如下所示。
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es2018",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
"include": [
"src/**/*"
]
}
package.json
{
"name": "typescript-express-boilerplate",
"version": "1.0.0",
"description": "Boilerplate project for TypeScript with ExpressJS.",
"engines": {
"node": "10.x"
},
"scripts": {
"start": "npm run serve",
"serve": "node dist/index.js",
"build": "npm run tslint && npm run build:ts",
"build:ts": "tsc",
"dev": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch:ts\" \"npm run watch:node\"",
"watch:node": "nodemon dist/index.js",
"watch:ts": "tsc -w",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"heroku-postbuild": "npm install && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"express": "^4.16.4",
"oauth2orize": "^1.11.0",
"passport": "^0.4.0",
"passport-http-bearer": "^1.0.1",
"pg": "^7.8.1",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.14"
},
"devDependencies": {
"@types/express": "^4.16.1",
"@types/node": "^11.10.4",
"@types/oauth2orize": "^1.8.5",
"@types/passport": "^1.0.0",
"@types/passport-http-bearer": "^1.0.33",
"concurrently": "^4.1.0",
"nodemon": "^1.18.10",
"tslint": "^5.13.0",
"typescript": "^3.3.3333"
}
}
index.ts
import "reflect-metadata";
import express from "express";
import { createConnection } from "typeorm";
import { User } from "./entity/User";
createConnection().then(async connection => {
const user = new User();
user.email = "hello@hello.com";
user.password = "password";
await connection.manager.save(user);
console.log("Saved a new user with id: " + user.id);
console.log("Loading users from the database...");
const users = await connection.manager.find(User);
console.log("Loaded users: ", users);
}).catch(error => console.log(error));
// Create a new express application instance
const app: express.Application = express();
app.get("/", function (req: express.Request, res: express.Response) {
res.send("Hello World!");
});
app.listen(process.env.PORT || 3000, function () {
console.log("Example index listening on port 3000!");
});
实体/User.ts
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({
length: 30
})
email: string;
@Column()
password: string;
}
提前致谢。
编辑:
编译后的 JavaScript 代码没有 ES6 import 语句,它使用 commonjs 模块:
index.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const express_1 = __importDefault(require("express"));
const typeorm_1 = require("typeorm");
const User_1 = require("./entity/User");
typeorm_1.createConnection().then(async (connection) => {
const user = new User_1.User();
user.email = "hello@hello.com";
user.password = "password";
await connection.manager.save(user);
console.log("Saved a new user with id: " + user.id);
console.log("Loading users from the database...");
const users = await connection.manager.find(User_1.User);
console.log("Loaded users: ", users);
}).catch(error => console.log(error));
// Create a new express application instance
const app = express_1.default();
app.get("/", function (req, res) {
res.send("Hello World!");
});
app.listen(process.env.PORT || 3000, function () {
console.log("Example index listening on port 3000!");
});
//# sourceMappingURL=index.js.map
实体/User.js
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
let User = class User {
};
__decorate([
typeorm_1.PrimaryGeneratedColumn(),
__metadata("design:type", Number)
], User.prototype, "id", void 0);
__decorate([
typeorm_1.Column({
length: 30
}),
__metadata("design:type", String)
], User.prototype, "email", void 0);
__decorate([
typeorm_1.Column(),
__metadata("design:type", String)
], User.prototype, "password", void 0);
User = __decorate([
typeorm_1.Entity()
], User);
exports.User = User;
//# sourceMappingURL=User.js.map
最佳答案
在项目根目录(package.json 附近)创建 ormconfig.json
。它应该包含以下内容:
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
}
关于javascript - TypeScript Typeorm Node模块导入错误: SyntaxError: Unexpected token {,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991994/
我正在开发一个应用程序,它使用 OAuth - 基于 token 的身份验证。 考虑到我们拥有访问和刷新 token ,这就是流程的样子。 Api call -> intercepter append
如何取消标记此代码的输出? 类(class)核心: def __init__(self, user_input): pos = pop(user_input) subject = ""
当我使用命令 kubectl 时与 --token标记并指定 token ,它仍然使用 kubeconfig 中的管理员凭据文件。 这是我做的: NAMESPACE="default" SERVICE
我正在制作 SPA,并决定使用 JWT 进行身份验证/授权,并且我已经阅读了一些关于 Tokens 与 Cookies 的博客。我了解 cookie 授权的工作原理,并了解基本 token 授权的工作
我正在尝试从应用服务获取 Google 的刷新 token ,但无法。 日志说 2016-11-04T00:04:25 PID[500] Verbose Received request: GET h
我正在开发一个项目,只是为了为 java 开发人员测试 eclipse IDE。我是java新手,所以我想知道为什么它不起作用,因为我已经知道该怎么做了。这是代码: public class ecli
我正在尝试使用 JwtSecurityTokenHandler 将 token 字符串转换为 jwt token 。但它出现错误说 IDX12709: CanReadToken() returned
我已阅读文档 Authentication (来自 Facebook 的官方)。我仍然不明白 Facebook 提供的这三种访问 token 之间的区别。网站上给出了一些例子,但我还是不太明白。 每个
我的部署服务器有时有这个问题,这让我抓狂,因为我无法在本地主机中重现,我已经尝试在我的 web.config 中添加机器 key ,但没有成功远。 它只发生在登录页面。 我的布局:
我已经设法获得了一个简单的示例代码,它可以创建一个不记名 token ,还可以通过阅读 stackoverflow 上的其他论坛来通过刷新 token 请求新的不记名 token 。 启动类是这样的
如果我有以前的刷新 token 和使用纯 php 的访问 token ,没有 Google Api 库,是否可以刷新 Google Api token ?我在数据库中存储了许多用户刷新和访问 toke
我通过 Java 应用程序使用 Google 电子表格时遇到了问题。我创建了应用程序,该应用程序运行了 1 年多,没有任何问题,我什至在 Create Spreadsheet using Google
当我有一个有效的刷新 token 时,我正在尝试使用 Keycloak admin REST API 重新创建访问 token 。 我已经通过调用 POST/auth/realms/{realm}/p
我正在尝试让第三方 Java 客户端与我编写的 WCF 服务进行通信。 收到消息时出现如下异常: Cannot find a token authenticator for the 'System.I
在尝试将数据插入到我的 SQl 数据库时,我收到以下错误 System.Data.SqlServerCe.SqlCeException: There was an error parsing the
使用数据库 session token 系统,我可以让用户使用用户名/密码登录,服务器可以生成 token (例如 uuid)并将其存储在数据库中并将该 token 返回给客户端。其上的每个请求都将包
我最近注册了 Microsoft Azure 并设置了认知服务帐户。使用 Text Translation API Documentation 中的说明我能够使用 interactive online
我使用 IAntiforgery API 创建了一个 ASP.Net Core 2 应用程序。 这提供了一种返回 cookie 的方法。 客户端获取该 cookie,并在后续 POST 请求中将该值放
我正在使用 spacy 来匹配某些文本(意大利语)中的特定表达式。我的文本可以多种形式出现,我正在尝试学习编写一般规则的最佳方式。我有如下 4 个案例,我想写一个适用于所有案例的通用模式。像这样的东西
我无法理解 oauth 2.0 token 的原则处理。 我的场景是,我有一个基于 web 的前端后端系统,带有 node.js 和 angular 2。用户应该能够在此站点上上传视频。然后创建一些额
我是一名优秀的程序员,十分优秀!