mongoose λ²μ 4.8.0 λ° 4.8.1 (λ Έλ λ²μ μ 6.9.5μ΄κ³ MongoDB λ²μ μ 3.2.11)μμλ μμ© νλ‘κ·Έλ¨μ μμν ν λ¬Έμλ₯Ό μ²μ μ μ₯ν λ λ€μ κ²½κ³ κ° μμ±λ©λλ€ (λ€μκ³Ό κ°μ κ²½μ°μλ κ²½κ³ κ° νμλμ§ μμ). μμ© νλ‘κ·Έλ¨μ΄ κ³μ μ€νλλ λμ μ²μμΌλ‘ λμΌν μ½λκ° μ΄νμ μ€νλμ§λ§ μμ© νλ‘κ·Έλ¨μ λ«μλ€κ° λ€μ μμνλ©΄ μμ© νλ‘κ·Έλ¨μ΄ μμλ ν μ²μμΌλ‘ λ¬Έμ μ μ₯μ΄ λ°μνλ©΄ κ²½κ³ κ° μμ±λ©λλ€.
DeprecationWarning : Mongoose : mpromise (mongooseμ κΈ°λ³Έ promise λΌμ΄λΈλ¬λ¦¬)λ λ μ΄μ μ¬μ©λμ§ μμ΅λλ€. λμ μμ μ promise λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°κ²°νμΈμ. http://mongoosejs.com/docs/promises.html
mongoose λ²μ 4.7.9 λ° μ΄μ λ²μ μμ λκ°μ μ ν리μΌμ΄μ μ½λλ₯Ό μ¬μ©ν λλμ΄ λ¬Έμ κ° λνλμ§ μμ΅λλ€.
λͺ½κ΅¬μ€ μ°κ²°μ μ€μ νλ μ½λλ λ€μκ³Ό κ°μ΅λλ€.
`// Set up mongoose and DB connection
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost:27017/basedir', {server: { poolSize: 5 }});`
μ€μ λ‘ μ€λ₯λ₯Ό μμ±νλ μ½λλ₯Ό λΆμ¬ λ£μ μ μμ΅λκΉ? μμ§ μ½μμ μ¬μ©νμ§ μμκΈ° λλ¬Έμ ν΄λΉ μ½λκ° μλλλ€ (κ²μ ν μ°κ²° μ€ λν«μ΄ κ²½κ³ λ₯Ό μμ±νλ€κ³ λ§νμ§ μλ ν).
μ°κ²° μ€ λν«μ κ²½κ³ λ₯Ό μμ±νμ§ μμ΅λλ€. λ€μ newuser μ μ₯ μ€ λν«μ κ²½κ³ λ₯Ό μμ±νλ μ½λμ΄μ§λ§ μ ν리μΌμ΄μ μ΄ μμλ ν μ½λκ° μ²μ μ€νλ λλ§ ν΄λΉλ©λλ€ (λ€μ λ§νμ§λ§ mongoose λ²μ 4.7.9 λ° μ΄μ λ²μ μμλ κ²½κ³ κ° λ°μνμ§ μμ).
` var curDate = new Date();
var newuser = new user(
{
"userName": userName,
"hashed_password": hashed_password,
"emailReg": false,
"email": "~",
"firstName": "",
"lastName": "",
"address.streetAddress": "",
"address.city": "",
"address.state": "",
"address.postalCode": "",
"phoneNumbers": [],
"accessLevel": 2,
"active": true,
"trialStartDate": curDate,
"maxStorageByteLimit": constants.maxStorageBytesPerUser,
"signUpDate": curDate,
"passwordResetDate": curDate,
"salt": temp,
"storageBytesUsed": 0
});
if (phoneNumberValid != false)
{
newuser.phoneNumbers.push({
"type": "mobile",
"number": contactPhoneNumber,
"primary": true
});
}
wlogger.crit("Create new user by UserName: " + userName);
newuser.save(function (err)
{
if (err)
{
wlogger.error(err + " - when saving user "
+ " creation by UserName: " + userName);
callback({
"response": false,
"res": "User creation failed. "
+ " Please try again or contact us at [email protected]"
});
}
else
{
wlogger.crit("Saved new user info for UserName: "
+ userName);
return callback({
"response": true,
"res": "Created user by User Name " + userName,
"user_id": newuser._id,
"last_modified_date": curDate
});
}
})`
μ¬μ©μ μ€ν€λ§λ λ€μκ³Ό κ°μ΅λλ€.
`var userSchema = mongoose.Schema({
token : String,
email: String,
userName: String,
deviceId: String,
devicePhoneNumber: String,
hashed_password: String,
hashed_admin_code: String,
maxStorageByteLimit: { type: Number, default: 1073741824 },
accessLevel: { type: Number, default: 2 },
lastAccessType: Number,
storageBytesUsed: { type: Number, default: 0 },
totalStorageBytesUsed: { type: Number, default: 0 },
deletedStorageBytesUsed: { type: Number, default: 0 },
salt : String,
salt1 : String,
temp_str: String,
syncInProcess: { type: Boolean, default: false },
syncStartDate: { type: Date, default: Date.now },
syncVersion : { type: Number, default: 0 },
active: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
emailReg: { type: Boolean, default: false },
regPending: { type: Boolean, default: false },
emailChangePending: { type: Boolean, default: false },
regCodeSendCount: { type: Number, default: 0 },
trialStartDate: Date,
signUpDate: Date,
passwordResetDate: Date,
lastLoginDate: Date,
lastModifiedDate: Date,
curLoginDate: Date,
apnDeviceTokens: [ String ],
curSessionIds: [ String ],
curIpAddr: { ipType: String,
upper64: Schema.Types.Long,
lower64: Schema.Types.Long },
firstName: String,
lastName: String,
address: { streetAddress: String,
city: String,
state: String, postalCode: String },
phoneNumbers: [ phoneNumbersSchema ],
categories: [ categoriesSchema ],
providerCustomers: [ customerSchema ],
serviceProviders: [ providerSchema ],
ipAddrs: [ ipAddrSchema ],
recentUploads: [ recentUploadSchema ],
recentUploadsGizmo: [ recentUploadSchema ],
recentUploadsLife: [ recentUploadSchema ],
recentUploadsVehicle: [ recentUploadSchema ],
recentUploadsMisc: [ recentUploadSchema ],
recentViews: [ recentViewsSchema ],
recentAdds: [ recentAddsSchema ],
recentAddedFiles: [ recentAddedFilesSchema ],
locations: [ locationSchema ],
inMessages: [ inMessageSchema],
outMessages: [ outMessageSchema ]
});`
mongoose v4.8.1 λ° node.js v7.5.0μμλ λμΌν λ¬Έμ λ₯Ό νμΈν μ μμ΅λλ€.
mongoose.Promise = global.Promise;
mongoose.connect(db.mongodb.uri, db.mongodb.options);
DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
@ stevenelson74708 μ€ λν«μ μ£Όμ μ κ°μ¬νμ§λ§ μ€ν€λ§ μ체λ μλ§λ λ¬Έμ κ° μλ κ²μ λλ€. λ΄κ° λ μ°Ύκ³ μλ κ²μ μ°κ²°μ΄ μ΄λ¦΄ λ / mongoose Promise μμ±μκ° μ€μ λμμ λ μ€ν€λ§λ₯Ό κ°μ Έμ€κ³ μ¬μ©νλ λ°©λ²μ λλ€.
μ€ν€λ§λ λ€μκ³Ό κ°μ΄ μ μΈ λ μ€ν€λ§ νμΌμμ λ΄λ³΄λ΄μ§λλ€.
`module.exports = mongoose.model('users', userSchema); `
μ€ν€λ§λ newuser.saveκ° λ€μκ³Ό κ°μ΄ νΈμΆλλ κ³³μμ μ¬μ©λλ νμΌλ‘ κ°μ Έμ΅λλ€.
`var user = require('config/models/users');`
첫 λ²μ§Έ κ²μλ¬Όμ νμλ μ°κ²° μ€ λν« (μ°κ²°μ΄ μ΄λ € μκ³ mongoose Promise μμ±μκ° μ€μ λ¨)μ μ ν리μΌμ΄μ μμ μ€ μ΄κΈ°νμ μ€νλλ app.js νμΌμμ λ°μν©λλ€.
λ λ²μ§Έ κ²μλ¬Όμ νμλ newuser.save μ€ λν«μ μ¬μ©μ μ€ν€λ§λ₯Ό κ°μ Έμ¨ νμΌμμ μ€νλκ³ newuser.saveλ μ¬μ©μκ° μμ© νλ‘κ·Έλ¨κ³Ό μνΈ μμ©νμ¬ νΉμ κ²½λ‘κ° μ€νλλλ‘ ν λ μ€νλ©λλ€. μλ£λμλ€.
μ΄ κ²½κ³ λ μμ ν λμΌν newuser.save μ½λκ° μ¬λ¬ λ² μ€ν λλλΌλ μμ© νλ‘κ·Έλ¨ μμ ν newuser.save μ½λκ° μ²μ μ€νλ λλ§ λ°μνλ©° mongoose λ²μ 4.7.9 λ° μ΄μ λ²μ μμλ κ²½κ³ κ° λ°μνμ§ μλ κ²μΌλ‘ 보μ λλ€.
@ stevenelson74708 μ± μμμ μ€μν μν μνλ λͺ¨λ κ΅¬μ± μμλ₯Ό νμΈν΄μΌν©λλ€. λ§μ μ¬λλ€μ΄ μ΅κ·Όμ κ΄ν λ¬Έμ λ₯Ό μ΄μμ΅λλ€, κ·Έλ¦¬κ³ λ΄ μκ° μ λΉμ μ΄ μ€μ ν μμλλ‘ λκ° μλͺ»νκ³ μμ΄λλλ‘ λͺ¨λ νλ νλκ° μλͺ» μ½μ μμ±μλ₯Ό μ€μ λ§μμΌνλ€ mongoose.Promise
.
μ½λλ₯Ό λ³Ό μμλ λ°©λ²μ΄ μμ΅λκΉ?
λ€μμ mongoose.Promise μ€μ μ ν¬ν¨νμ¬ mongoose ꡬμ±κ³Ό κ΄λ ¨λ app.js μ΄κΈ°νμ λͺ¨λ μ½λ (μ νν μμ)μ λλ€. μ΄κΈ°ν μ½λκ° μ€νλ λ κ²½κ³ κ° λ°μνμ§ μμ΅λλ€. κ²½κ³ λ μμ© νλ‘κ·Έλ¨μ΄ μ΄κΈ°ν λ ν newuser.save μ½λ (μ΄μ κ²μλ¬Όμ νμλ¨)κ° μ²μ μ€νλ λλ§ λ°μν©λλ€. κ²½κ³ λ₯Ό λ°μμν€μ§ μλ μ μ₯ μ μ μ€νλλ findOneAndUpdate νΈμΆμ΄ μμΌλ―λ‘ κ²½κ³ λ₯Ό λ°μμν€λ κ²μ μ μ₯ κΈ°λ₯ λΏμΈ κ²μ²λΌ 보μ λλ€.
λͺ½κ΅¬μ€ ꡬμ±μ μ΄κΈ°νμμ κ°μ₯ λ¨Όμ λ°μνλ κ² μ€ νλμ λλ€. λͺ½κ΅¬μ€ ꡬμ±μ createServer κΈ°λ₯ μ΄νμ κ°μ μ΄κΈ°νμμ λμ€μ λ°μν΄μΌν©λκΉ?
λ€μ λ§νμ§λ§, mongoose λ²μ 4.7.9 λ° μ΄μ λ²μ μμλ κ²½κ³ κ° λ°μνμ§ μλ κ² κ°μ΅λλ€.
`// Set up mongoose and DB connection
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost:27017/basedir', {server: { poolSize: 5 }});
var conn = mongoose.connection;
conn.once('open', function ()
{
wlogger.info("Mongoose connection opened on process " + process.pid);
});`
μλμ, ꡬμ±μ΄ λ¨Όμ μ¬ μ μμ΅λλ€. μ΄ νμΌμ΄ μ±μ μ§μ μ μ λκΉ, μλλ©΄μ΄ μμ μ€λ νμΌμ΄ μμ΅λκΉ?
app.js νμΌμ μ ν리μΌμ΄μ μ μ§μ μ μ λλ€. λͺ λ Ή μ€μ node app.jsλ₯Ό μ λ ₯νμ¬ μ ν리μΌμ΄μ μ νΈμΆ ν μ μμ΅λλ€.
mongoose 4.8.1 λ° node 7.5.0κ³Ό λμΌν λ¬Έμ κ° μμ΅λλ€. λλ λν Typescriptλ₯Ό μ¬μ©νκ³ μμΌλ©° μ½μμ μν΄ Bluebirdλ₯Ό μ°κ²°νλ €κ³ ν©λλ€. μ΄ μ½λλ₯Ό μΆκ°νλ©΄ κ²½κ³ κ° μ¬λΌμ§λ€λ κ²μ μμμ΅λλ€.
`import * as mongoose from 'mongoose';
import * as bluebird from 'bluebird';
(<any>mongoose).Promise = bluebird;`
'λͺ½κ΅¬μ€'μμ 무μΈκ°λ₯Ό κ°μ Έ μ€λ λͺ¨λ νμΌμμ. λ°λΌμ μ§μ μ νμΌμμλ§ λμ λͺ¨λ λͺ¨λΈ νμΌ (λ΄ μ€ν€λ§λ₯Ό μ μνλ νμΌ)μμμ΄λ₯Ό μνν΄μΌν©λλ€.
// Set up mongoose and DB connection
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost:27017/basedir', {server: { poolSize: 5 }});
var conn = mongoose.connection;
conn.once('open', function ()
{
console.log('test');
});
μ΄ κ²½κ³ λ₯Ό μΈμνμ§ μμΌλ―λ‘ λ€λ₯Έ κ²μ΄ μμ΅λλ€.
@vladimirdjurdjevic μ΄κ²μ μ½μ libλ₯Ό μ€μ νκΈ° μ μ λͺ½κ΅¬μ€ λΉλκΈ° μμ μ μ¬μ©νκ³ μμμ μλ―Έν©λλ€. λλ TSλ₯Ό μ¬μ©νμ§ μκΈ° λλ¬Έμ κ·Έκ²μ΄ κ·Έκ²κ³Ό κ΄λ ¨μ΄ μλμ§ λͺ¨λ₯΄κ² μ΅λλ€.
μμ κ°μ΄ μ ν리μΌμ΄μ μ μ§μ μ μΈ app.js νμΌμμ μμ κ°μ΄ promise libλ₯Ό μ€μ ν λ€μ, μμ κ°μ΄ 첫 λ²μ§Έ newuser.saveλ₯Ό μννλ©΄ (promise libκ° μ€μ λ ν μ€λ) κ²½κ³ κ° λ°μν©λλ€. newuser.saveλ λ°μνλ 첫 λ²μ§Έ λͺ½κ΅¬μ€ λΉλκΈ° μμ μ΄μ§λ§ μ°κ²°μ λν΄ libκ° μ€μ λ μ½μ μ΄νμ λ°μν©λλ€.
λͺ½κ΅¬μ€ μ°κ²°μ΄ κ΅¬μ± λ λ μ½μ λΌμ΄λΈλ¬λ¦¬λ₯Ό ν λ²λ§ μ€μ νλ κ²κ³Ό λ°λλ‘ λͺ½κ΅¬μ€ λΉλκΈ° κΈ°λ₯μ μ‘μΈμ€νλ κ° νμΌμ promise libλ₯Ό μ€μ ν΄μΌν©λκΉ?
@ stevenelson74708 μ λ κ°μ λ¬Έμ κ°μμμ΅λλ€. μμ© νλ‘κ·Έλ¨μ μ§μ
μ μμ mongoose.Promise = global.Promise
λ‘ ν΄κ²°νλ€κ³ μκ°νμ΅λλ€. λͺ½κ΅¬μ€ μ½μμ μ‘μΈμ€νλ λͺ¨λ νμΌμ νμν κ² κ°μ΅λλ€. κ½€ μ§μ¦λ. λͺ½κ΅¬μ€ ꡬμ±μ λ³λμ νμΌλ‘ μ€μ νκ³ λ΄λ³΄λ΄κ³ νμν λλ§λ€ κ°μ Έ μ€λ©΄λ©λλ€.
λν μ΅κ·Όμ app.js νμΌμ μ½μμ μ€μ νμ§λ§μ΄ κ²½κ³ κ° νμλλ κ²μ νμΈνμ΅λλ€. λμΌν κ²°κ³Όλ‘ λ€μ΄ν°λΈμ λΈλ£¨ λ²λλ₯Ό λͺ¨λ μ¬μ©ν΄ 보μμ΅λλ€.
// FILE: app.js
import bluebird from 'bluebird';
import mongoose from 'mongoose';
// use bluebird as default promise library
mongoose.Promise = bluebird;
mongooseλ₯Ό λ΄ λͺ¨λΈ μ€ λͺ κ°λ‘ κ°μ Έμ μ€ν€λ§ λ©μλμμ μ¬μ©νκ³ μμ΅λλ€. λͺ½κ΅¬μ€λ₯Ό μ체μ μΌλ‘ κ°μ Έ μ€λ νμΌμ promise λΌμ΄λΈλ¬λ¦¬λ₯Ό μ€μ νμ¬ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€ (μλ μ€ λν«μ TODO μ°Έμ‘°). λΉμ·ν μ½λ κ΅¬μ‘°κ° μ£Όμ΄ μ‘μ λ λͺ λ¬ μ μ κ²½κ³ λ₯Ό λ³Έ κΈ°μ΅μ΄ μμ΅λλ€.
// FILE: SessionModel.js
import bluebird from 'bluebird';
import mongoose from 'mongoose';
// TODO: remove this redundant bit of code
// use bluebird as default promise library
mongoose.Promise = bluebird;
SessionSchema.methods.getUser = function(callback) {
return mongoose.model('User').findById(this.user, callback);
};
@mcfarljw λ€μκ³Ό κ°μ΄νλ κ²μ΄ μ’μ΅λλ€.
// config.js
const bluebird = require('bluebird');
const mongoose = require('mongoose');
mongoose.Promise = bluebird;
mongoose.connect('mongodb://localhost:27017/myApp)
module.exports = { mongoose }
// SessionModel.js
const { mongoose } = require('./config')
SessionSchema.methods.getUser = function(callback) {
return mongoose.model('User').findById(this.user, callback);
};
μ΄λ κ²νλ©΄ "mongoose.Promise = bluebird"λ₯Ό μ¬λ¬ λ² μΈ νμκ° μμ΅λλ€. :)
λ΄ μν©μμλ .then()
ꡬ문μ μ¬μ©νμ¬ νμ λ¬Έμμμ μμ
ν λ μ¬μ© μ€λ¨ κ²½κ³ κ° νμλμ§λ§ λΆλͺ¨ λ¬Έμμμλ κ·Έλ μ§ μμ΅λλ€. κ·Έλμ κ°λμ© κ²½κ³ λ₯Ό λ°μΌλ©΄ μ΄λ¦°μ΄ λ¬Έμλ₯Ό μ²λ¦¬νλ λ° κ΅¬λ¬Έ λ¬Έμ κ° μλ€κ³ μκ°νμ§λ§ μ€μ λ‘λ λ€λ₯Έ μ¬λλ€μ΄ μμμ μ‘°μΈ ν κ²μ²λΌ λͺ¨λΈ νμΌμ μ½μ μ€μ΄ μμμ΅λλ€.
μ, mongoose.modelμ λ΄ λ³΄λΈ μ€ν€λ§ νμΌμμ mongoose.Promiseλ₯Ό μ€μ νλ©΄ mongoose λ²μ 4.8 μ΄μμμ κ²½κ³ κ° μ κ±°λλ κ²μΌλ‘ 보μ λλ€.
λ€ μμ μ λ³ν λ λ΄λ³΄λ΄κΈ°λ‘ μΈν΄ λͺ½κ΅¬μ€ μ±κΈ ν€μ λ겨 μ£Όλ©΄ λ¬Έμ κ° ν΄κ²° λ κ²μ λλ€. κ·Έλ¬λ κ·Έκ²μ μ½κ°μ ν΄νΉμ λλ€.
μ΄ μμ μμ κ²½κ³ λ₯Ό νΌνκΈ°μν "μ μ ν"λͺ½κ΅¬μ€ μ€μ / ꡬμ±μ΄ 무μμΈμ§ μ νν μ μ μμ΅λλ€. μ΄ κ²½κ³ λ νμ¬ λͺ½κ΅¬μ€ λ²μ μ λ¬Έμ μ λκΉ μλλ©΄ λΆμ μ ν λͺ½κ΅¬μ€ μ€μ μ λνλ΄λ κ²μ λκΉ? κ²½κ³ λ₯Ό λ°©μ§νλ κΆμ₯ λͺ½κ΅¬μ€ μ€μ μ΄ μμ΅λκΉ? λ€μ λ§νμ§λ§,μ΄ κ²½κ³ λ mongoose λ²μ 4.8.0μμλ§ λ°μν©λλ€.
κΈλ‘λ² λͺ½κ΅¬μ€λ₯Ό μ€μ νλ λ° λμΌν λ¬Έμ κ° μμ΅λλ€.
μΆμ ν΄λ³΄λ©΄ bluebirdλ₯Ό promise_providerλ‘ μ μ νκ² μ€μ ν μ μμ§λ§ promiseλ₯Ό λλλ¦¬λ € κ³ νλ©΄ ES6μ΄ λ°νλ©λλ€.
λλ λ°©κΈ ν΅κ³Όνκ³ μΆκ°νμ΅λλ€.
mongoose.Promise = global.Promise
λͺ½κ΅¬μ€λ₯Ό μ¬μ©νλ λͺ¨λ μμ€ νμΌμμ λͺ½κ΅¬μ€μ μꡬ / κ°μ Έ μ€κΈ°λ₯Ό μ¦μ μν ν μ μμΌλ©° κ³Όλνμ§λ§ μκ°μ΄ κ±°μ λ€μ§ μκ³ μκ°μ΄ λ§μ΄ κ±Έλ¦¬μ§ μμμΌλ©° μμ ν μμ λμμ΅λλ€. λ°λΌμ μ€μ λ‘ κ°μ₯ ν° κ±°λλ μλλλ€.
μμ μ μꡬνκ³ κ°μ Έ μ€λ λ°©λ²μ λν μ νλ μ§μμ λμ ν©λλ€. λͺ λ²μ΄λΌλ νμν κ²μ΄ μμ΄λ νμ κ°μ μΈμ€ν΄μ€λ₯Ό κ°μ§λ―λ‘ μ½μμ ν λ²λ§ μ€μ νλ©΄ μΆ©λΆνλ€κ³ μκ°ν©λλ€. κ·Έλμ κ·Έκ²μ μ½κ° λ° μ§κ΄μ μ λλ€.
μλ
νμΈμ
λ¬Έμ λ₯Ό μΌμΌν€λ λͺ κ°μ§ μμ μ½λκ° μμ΅λλ€.
import mongoose from 'mongoose';
mongoose.Promise = Promise;
import Test from './test.model';
mongoose.connect(uri, options);
mongoose.connection.on('error', err => console.error(`MongoDB connection error: ${err}`));
Test.create({ name: 'Hi'})
.then(() => Test.findOne({}))
.then(res => res.save())
.then(() => console.log('Done'));
(test.modelμ μ΄λ¦ νλκ° νλ μΈ λ¨μν λͺ¨λΈμ λλ€)
μ΄λ€ μ΄μ λ‘ res.save();
ν λ λ¬Έμ κ° μ²μ λνλ©λλ€. κ·Έκ² μμ΄λ λͺ¨λ κ²μ΄ νμλλ‘ μλν©λλ€.
μ΄ λ¬Έμ λ μ½λλ₯Ό νΈλμ€ νμΌ ν λ λ°λ²¨μ΄ μμλ₯Ό λ³κ²½νλ λ°μ λΉλ‘―λ κ²μ΄λΌκ³ μκ°ν©λλ€. λ°λ²¨μ΄ νΈλμ€ νμΌ ν λ λͺ¨λ κ°μ Έ μ€κΈ°λ₯Ό 맨 μλ‘ μ΄λν©λλ€. λ°λΌμ λ΄ μμ μμ test.modelμ κ°μ Έμ¬ λ Promise-libraryλ μ¬μ ν mpromiseμ ν λΉλ©λλ€.
Node.js 6.10.0κ³Ό Mongoose 4.8.5λ₯Ό μ¬μ©νκ³ μλλ°μ΄ λ¬Έμ κ° λ°μνμ΅λλ€.
λ΄ μ¬μ©μ κ°μ²΄λ₯Ό μ μ₯ν©λλ€.
var newUser = User({
login : 'john',
password : 'secret',
firstname : 'John',
lastname : 'Doe',
avatar : 'avatar.png'
});
newUser.save(function(err){
if(err) throw err;
console.log('User created !');
});
κ·Έλ¦¬κ³ λλ κ°μ μ€λ₯κ° μμ΅λλ€. λλ μ΄λ κ² μλνλ€ :
mongoose.Promise = require('bluebird');
mongoose.connect(conf.dbpath);
κ·Έλ¦¬κ³ μ΄λ κ² :
mongoose.Promise = global.Promise;
mongoose.connect(conf.dbpath);
κ·Έλ¬λ λ€μ μ¬μ©λμ§ μλ κ²½κ³ κ° μμ΅λλ€. μ΄ λ¬Έμ λ₯Ό μ΄λ»κ² ν΄κ²°ν μ μμ΅λκΉ?
μ§μ μ€λ¨ κ²½κ³ μμ λ°©λ² :
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var schema = new mongoose.Schema({
// definition
}, {
// options
});
module.exports = mongoose.model('foobar', schema);
μ, μ΄κ²μ λ΄ λͺ¨λΈμ λ£μΌλ©΄ μλν©λλ€. κ·Έλμ μ°λ¦¬λ mongoose.Promiseλ₯Ό κ° λͺ¨λΈ λ΄μμ μ΄κΈ°νν΄μΌν©λλ€.
At this point it's unclear to me as to exactly what the "proper" mongoose setup/configuration to avoid the warning is.
λΌκ³ λ§ν λ @ stevenelson74708 μ΄ νλ₯ν ν¬μΈνΈλΌκ³ μκ°ν©λλ€.
μ, μμ μ λͺ¨λ λͺ½κ΅¬μ€ λͺ¨λΈ νμΌλ‘ λμκ°μ κ°μ Έ μ€κΈ° νμ mongoose.Promise = global.Promise
λ₯Ό μΆκ°νλ κ²μ΄μ§λ§,μ΄ λ¬Έμ λ μ΅κ·ΌμμΌ λ°μνκΈ° λλ¬Έμ μλμ λν΄λ³΄λ€ ꡬ체μ μΈ λ΅λ³μ μ»λ κ²μ΄ μ’μ΅λλ€. λλ κ·Έ λ€μ μμΈ. μ΄ μμ μμ κ·Έκ²μ΄ λͺ½κ΅¬μ€, λ°λ²¨ λλ λ κ°μ§μ μ‘°ν©μΌλ‘ μΈν λ¬Έμ μΈμ§ νμ€νμ§ μμ΅λλ€.
# 5030μ μμμ μΌ κ°λ₯μ±μ΄ μμ΅λλ€. > = 4.9.1λ‘ μ κ·Έλ μ΄λνμμμ€.
κΈ°λ‘μ μν΄ @bricss μ루μ μ λλ₯Ό μν΄ κ½€ μ μλνμ΅λλ€. :)
μ²μμ ν΄κ²°μ±
μ @ steve-p-comμμ λμμ΅λλ€. @bricssκ° μλλλ€.
@ vkarpov15 4.9.3κ³Ό λμΌν κ²½κ³ .
@stillesjo λ°λ²¨κ³Όλ μ무κ²λ μλλλ€. κ²½κ³ λ λ
Έλ 7.8.0μ Babelμ΄μλ nativμμλ λ°μν©λλ€.
λ²μ 4.9.3 μ΄ν κ²½κ³ κ° μμ΅λλ€ π·
λͺ¨λ λͺ¨λΈμ mongoose.Promise = global.Promise;
λ₯Ό μ€μ ν΄λ _ideal_ fixκ° μλλΌκ³ μκ°ν©λλ€. global.Promise
λ₯Ό κΈ°λ³Έκ°μΌλ‘ μ¬μ©νμ§ μκ³ μ¬μ©μκ° μνλ κ²½μ° λ³κ²½ν μμλ μ΄μ λ 무μμ
λκΉ?
λ΄ λͺ¨λΈ λ¨μ ν
μ€νΈ. λ‘컬 λλ CIμμ ν
μ€νΈλ₯Ό μ€νν λ κ²½κ³ κ° νμλμ§ μλλ‘ ν¬ν¨ν΄μΌν©λλ€.
λ΄κ° μ¬μ©νκ³ "mongoose": "4.10.4"
λ
Έλ v7.10.0
λΏ‘ λΉ΅λ¨
μ΄ DRY μ루μ μ μΆκ°νλ €λ©΄ κ° λͺ¨λΈ λ΄λΆμ μ€μ νλ λμ λ°μ΄ν°λ² μ΄μ€μμ μ°κ²°μ μ΄κΈ°ννλ μμΉλ₯Ό μ€μ νμμμ€.
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://...');
μλνλ κ² κ°λ€
κ·Έλλ λͺ¨λ μ¬λμκ² λ°λμ ν¨κ³Όκ°μλ κ²μ μλλλ€. μ½λ ꡬ쑰μ λ°λΌ λ€λ¦ λλ€. μλ₯Ό λ€μ΄ μ°κ²° μ½λκ°μλ νμΌμ λ³λμ μμ€ νμΌμ λͺ¨λΈμ΄ νμν κ²½μ° μμ μꡬ λ°©μμ μ°κ²° μ½λκ° ν¬ν¨ λ νμΌμ μ μ μ½μμ΄ μ€μ λκΈ° μ μ ν΄λΉ μμ€ νμΌμ΄λ‘λλ¨μ μλ―Έν©λλ€. μ΄λ¬ν λͺ¨λΈμ μ½μμ λν κΈ°λ³Έκ°μ κ³μ μ¬μ©ν©λλ€. μμ ν κ²½λ‘λ λ¨μν λͺ½κ΅¬μ€κ° νμν λͺ¨λ μμ€ νμΌμμ μ μ μ½μμ μ€μ νλ κ²μ λλ€.
μ΄κ²μ λλ₯Ό μν΄ μλν©λλ€
````
'λͺ½κ΅¬μ€'μμ λͺ½κ΅¬μ€ κ°μ Έ μ€κΈ°;
'./config'μμ κ΅¬μ± κ°μ Έ μ€κΈ°;
'bluebird'μμ bluebird κ°μ Έ μ€κΈ°;
κΈ°λ³Έ μ½λ°± λ΄λ³΄λ΄κΈ° => {
mongoose.Promise = λΈλ£¨ λ²λ;
// κ΅¬μ± νμΌμ λ°μ΄ν°λ² μ΄μ€ μΈμ€ν΄μ€μμ λ°μ΄ν°λ² μ΄μ€μ μ°κ²°
let db = mongoose.connect (config.mongoUrl, {useMongoClient : true});
μ½λ°± (db);
}
````
κ·Έκ² μ κ°νκ³ μλ λ°©μμ λλ€.
var colors = require('colors');
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/db', { useMongoClient: true })
.then(function(){
console.log(" Connected to dbName ".green);
}).catch(err => console.error(err));
.saveκ° μλ mongooseμ ν¨κ» μ 곡λλ .create ν¨μλ₯Ό μ¬μ©ν΄μΌν©λλ€.
λλ λ΄ κ²μ λ°κΎΈκ³ λ¬Έμ λ₯Ό ν΄κ²°ν©λλ€.
λλ 4.13.9μ΄κ³ μ΄κ²μ μ¬μ ν ββμ‘΄μ¬ν©λλ€. @ Gilbert136 μ루μ μ μ΄κ²μ μμ νμ§λ§ μ΄μνκ² μκ°ν©λλ€. .create νΈμΆ .save, κ·Έλ λ€λ©΄ μ§μ μ€λ¨ κ²½κ³ κ°μλ μ΄μ λ 무μμ λκΉ?
κ°μ₯ μ μ©ν λκΈ
μ§μ μ€λ¨ κ²½κ³ μμ λ°©λ² :