本文整理匯總了TypeScript中mongoose.Promise類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Promise類的具體用法?TypeScript Promise怎麽用?TypeScript Promise使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Promise類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: function
schema.method('promiseToSave', function(next) {
let promise = new Promise();
this.save(function(err, document) {
if(err) {
promise.reject(err);
} else {
promise.fulfill(document);
}
});
return promise;
});
示例2:
STSchema.path('name').index(true).index({ unique: true, sparse: true });
schematype.required(true, 'mess').required(true);
schematype.select(true).select(false);
STSchema.path('name').set(cb).set(cb);
schematype.sparse(true).sparse(true);
schematype.text(true).text(true);
schematype.unique(true).unique(true);
schematype.validate(/re/)
.validate({}, 'error')
.validate(cb, 'try', 'tri');
/*
* section promise.js
* http://mongoosejs.com/docs/api.html#promise-js
*/
var mongopromise = new mongoose.Promise();
mongopromise = new mongoose.Promise(function (err: any, arg: any) {
arg.sparse(true);
err.stack;
});
mongopromise = new mongoose.Promise(function (err: any, arg1: any, arg2: any) {
arg1.sparse(true);
arg2.sparse(true);
err.stack;
});
mongopromise.addBack(function (err: any, arg: any) {
err.stack;
arg.sparse(true);
}).addBack(function (err: any, arg1: any, arg2: any) {
err.stack;
arg1.sparse(true);
示例3: Error
mongopromise.on('init', cb).on('init', cb);
mongopromise.reject({}).reject('').reject(new Error('hi'));
mongopromise.resolve(new Error('hi'), {}).resolve();
mongopromise.then(function (arg) {
arg.toFixed();
return 9;
}, function (err) {
err.stack;
return 9;
}).then(function (arg1, arg2) {
arg1.toFixed();
arg2.toFixed();
});
mongopromise.complete();
/* static properties */
mongoose.Promise.ES6(function (complete: any, error: any) {
complete.apply(this);
error.apply(this);
});
/* Practical Examples */
interface IUser extends mongoose.Document {
name: string;
age: number;
}
var UserSchema = new mongoose.Schema({
name: String,
age: Number
});
var UserModel: mongoose.Model<IUser> = mongoose.model<IUser>('Model', UserSchema);
UserModel.findOne({}).exec().fulfill();
UserModel.find({}).exec().then(() => {}).catch(() => {}).reject('');
示例4:
this.save(function(err, document) {
if(err) {
promise.reject(err);
} else {
promise.fulfill(document);
}
});