本文整理汇总了TypeScript中mongoose.connection.once方法的典型用法代码示例。如果您正苦于以下问题:TypeScript connection.once方法的具体用法?TypeScript connection.once怎么用?TypeScript connection.once使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoose.connection
的用法示例。
在下文中一共展示了connection.once方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: resolve
return new Promise<DatabaseObject>((resolve, reject) => {
mongoose.connect(uri, config)
mongoose.connection.once('connected', () => {
logger.info('database connection: success')
resolve({
alert: AlertModel,
attachment: AttachmentModel,
connection: mongoose.connection,
delivery: DeliveryModel,
logger: logger,
order: OrderModel,
place: PlaceModel,
product: ProductModel,
stock: StockModel,
tokenSalt: config.tokenSalt,
user: UserModel
})
})
mongoose.connection.on('error', (err) => {
logger.error(`database error: ${err}`)
if (! this.isResolved) {
reject(err)
}
})
mongoose.connection.on('disconnected', () => {
logger.info('database connection: ended')
})
})
示例2: config
private config() {
this.app.use(Middleware.configuration);
mongoose.connection.once('open', () => {
console.log('Connected to Mongoose at', environment.mongoConnectionString);
});
mongoose.connect(environment.mongoConnectionString);
}
示例3: configureDB
//<% if (useDB) { %>
/**
* Connecting to database
*/
configureDB() {
// Dealing with Database
const dbName = '<%= dbName %>';
//<% if (dbType === 'mongo') { %>
const mongoURL = `mongodb://${process.env.MONGOHOST ? process.env.MONGOHOST : 'localhost'}/${dbName}`;
mongoose.connect( mongoURL, { useMongoClient: true } );
mongoose.connection.once( 'open', () => {
let Blog = mongoose.model( 'Blog' );
let User = mongoose.model( 'User' );
let Post = mongoose.model( 'Post' );
let userDoc = new User( {
firstName: 'Tata',
lastName: 'Toto'
} );
let blogDoc = new Blog( {
name: 'My Blog',
creator: userDoc._id
} );
let postDoc = new Post( {
author: userDoc._id,
blog: blogDoc._id
} );
userDoc.save( ( err: Error, user: UserDoc ) => {
this.debug( user );
} );
blogDoc.save( ( err: Error, blog: BlogDoc ) => {
this.debug( blog );
} );
postDoc.save( ( err: Error, post: PostDoc ) => {
this.debug( post );
Blog.find( {} ).populate( 'posts creator' ).exec( ( error: Error, blogs: BlogDoc[] ) => {
this.debug( blogs );
} );
} );
} );
/*const store = new MongoDBStore(
{
uri: mongoURL,
collection: 'sessions'
}
);
store.on('error', function (error) {
// Also get an error here
});*/
//<% } %>
}
示例4: router
router(app);
app.all('/ng-example*', (req, res) => {
res.sendFile('index.html', {root: pathToNgPublic});
});
app.all('/ng2-example*', (req, res) => {
res.sendFile('index.html', {root: pathToNg2Public});
});
app.all('/ng-upgrade-example*', (req, res) => {
res.sendFile('index.html', {root: pathToNgUpgradePublic});
});
app.all('/*', (req, res) => {
res.sendFile('landing.html', {root: __dirname + '/..'});
});
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/ng-upgrade-profiles');
mongoose.connection.once('open', () => {
console.log('Mongoose connected to ng-upgrade-profiles db at "localhost:27017"');
});
app.listen('8787', function listener() {
console.log('Express app listening at "http://localhost:8787/"');
});
export { mongoose };
示例5: require
import * as helmet from 'helmet';
import * as morgan from 'morgan';
import * as mongoose from 'mongoose';
import {json, urlencoded} from 'body-parser';
const config = require('../../tradejs.config');
const app = express();
app.listen(config.server.email.port, () => console.log(`\n Email service started on : 127.0.0.1:${config.server.email.port}`));
/**
* mongo
*/
mongoose.set('debug', true);
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function () {
console.log('DB connected');
});
mongoose.connect(config.server.email.connectionString);
/**
* express
*/
app.use(morgan('dev'));
app.use(helmet());
app.use(json());
app.use(urlencoded({extended: false}));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '_id, Authorization, Origin, X-Requested-With, Content-Type, Accept');
next();
});