我试图使用Mongoose在MongoDB中保存一个保存,但我得到的代码不起作用,所以我谷歌那个,我找到了一个有效的解决方案,但是tyescript一直告诉我,有一个错误。
但代码实际上有效。
这是不起作用的代码,但根据打字稿,没关系:
userSchema.static('createUser', (user: IUser, callback: Function) => {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(user.password, salt, (err, hash) => {
if(err) throw err;
user.password = hash;
user.save({}, (err, doc) => callback);
});
});
});
这是正在运行的代码,但是typescriot向我显示了一个我无法用tsc编译的错误(我可以用gulp构建来完成)
userSchema.static('createUser', (user: IUser, callback: Function) => {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(user.password, salt, (err, hash) => {
if(err) throw err;
user.password = hash;
user.save(callback);
});
});
});
使用第一个选项时,问题是保存中没有触发回调。
这是我的package.json
{
"name": "sss",
"version": "1.0.0",
"description": "Example App uses TypeScript, Node.js, Express 4, MongoDB, Mongoose.",
"main": "index.js",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.17.2",
"debug": "^2.6.8",
"dotenv": "^2.0.0",
"express": "^4.15.3",
"iconv-lite": "^0.4.17",
"inversify": "^4.11.1",
"jsonwebtoken": "^7.4.1",
"mongoose": "^5.0.10",
"morgan": "^1.8.2",
"passport": "^0.3.2",
"passport-jwt": "^2.2.1",
"rxjs": "^5.4.2",
"typescript": "^2.7.2",
"winston": "^2.3.1"
},
"devDependencies": {
"@types/chai": "^4.0.0",
"@types/debug": "0.0.29",
"@types/dotenv": "^2.0.20",
"@types/express": "^4.0.35",
"@types/mocha": "^2.2.41",
"@types/mongoose": "^4.7.15",
"@types/morgan": "^1.7.32",
"@types/node": "^6.0.77",
"@types/passport": "^0.3.3",
"@types/passport-jwt": "^2.0.20",
"chai": "^4.0.2",
"chai-http": "^3.0.0",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-typescript": "^3.1.7",
"gulp-yaml": "^1.0.1",
"mocha": "^3.4.2",
"mocha-typescript": "^1.1.4"
},
"scripts": {
"start": "tsc && nodemon src/index.ts",
"build": "gulp build",
"test": "tsc && mocha -t 30000 dist/**/*.test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/devslaw/TypeScript-Node.js-REST-example.git"
},
"keywords": [],
"author": "Arthur Arakelyan <arthur@devslaw.com>",
"license": "ISC",
"homepage": "https://github.com/devslaw/TypeScript-Node.js-REST-example#readme"
}
---更新---
IUSER
import {Schema, Model, Document, model} from 'mongoose';
import * as bcrypt from 'bcryptjs';
export interface IUser extends Document {
name: string;
email: string;
password: string;
}
export interface IUserModel {
createUser(user: IUser, callback: Function): void
comparePassword(candidatePassword: string, hash: string, callback: Function): void
findByEmail(email: string, callback: Function): void
}
const userSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
createAt: {
type: Date,
"default": Date.now()
},
updatedAt: {
type: Date,
"default": Date.now()
}
});
userSchema.static('createUser', (user: IUser, callback: Function) => {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(user.password, salt, (err, hash) => {
if(err) throw err;
user.password = hash;
user.save(callback);
});
});
});
userSchema.static('comparePassword', (candidatePassword: string, hash: string, callback: Function) => {
bcrypt.compare(candidatePassword, hash, (err, isMatch) => {
if(err) throw err;
callback(null, isMatch);
});
});
userSchema.static('findByEmail', (email: string, callback: Function) => {
User.findOne({email: email}, callback);
});
export type UserModel = Model<IUser> & IUserModel & IUser;
export const User: UserModel = <UserModel>model<IUser>("User", userSchema);
错误是:
src/shared/models/user.ts(44,23): error TS2345: Argument of type 'Function' is not assignable to parameter of type '(err: any, product: IUser, numAffected: number) => void'.
Type 'Function' provides no match for the signature '(err: any, product: IUser, numAffected: number): void'.
答案 0 :(得分:1)
根据您获得的错误,user.save
方法预计会收到类型为的回调:
(err: any, product: IUser, numAffected: number) => void
由于Function
类型非常宽泛并且基本上包含任何可调用类型,因此无法保证Function
的某些内容会接受上面较窄类型中列出的参数。
要解决此问题,您应该将callback
参数限制为较窄的类型。当您将错误传递给user.save
时,这将删除错误,并确保您向createUser
提供的任何回调都是正确的形式(因为目前您可以将任何函数传递给它,即使它& #39;不是正确的类型。)
因此,对于您的代码段,请更改以下行:
userSchema.static('createUser', (user: IUser, callback: Function) => {
到
userSchema.static('createUser', (user: IUser, callback: (err: any, product: IUser, numAffected: number) => void) => {
这样回调就是更具体的类型。