2015-11-13 27 views
-1

我是一個初學者,使用nodejs和mongoose。如何使用貓鼬先保存並在下面找到

所以我有一個這樣的麻煩:

後,創建一個架構模型,並把數據並保存,我想再次找到。

var mongoose = require("mongoose"); 
var Schema = mongoose.Schema; 
var typePlantSchema = new Schema({ 
    namePlant:String, 
    estimateDuration:Number, 
    conditionPh:Number 

}); 


var typePlantModels= mongoose.model('typePlant',typePlantSchema); 
var typePlantModel = new typePlantModels(); 
mongoose.connect('mongodb://localhost/AeroDB', function (err) { 
    if(err) { 
     console.log('connection error', err); 
    } else { 
     console.log('connection successful'); 
    } 
}); 
typePlantModel.namePlant='tam'; 
typePlantModel.estimateDuration=10; 
typePlantModel.conditionPh=7; 
typePlantModel.save(); 

typePlantModels.find({namePlant:"tam"},function(err,docs){ 
    if(err){ 
     console.log(err); 
    } 
    else {console.log(docs);} 
}); 

但是當我運行我的代碼第一次,我不能得到任何結果發現。如果再運行一次,我發現1個結果(事件2結果)。這意味着查找功能超過保存功能之前。我認爲這取決於回調函數的順序。那麼你可以有任何解決方案使它工作?的coure,而不是把find函數放在save函數中。 請幫我做到這一點。

更新更多的問題

其實,我想將它轉移到DATABSE API。它看起來像這樣:

var typePlantModels= mongoose.model('typePlant',typePlantSchema); 
var typePlantModel = new typePlantModels(); 
typePlant.prototype=typePlantModel; 
typePlant.prototype.findByName=function(name){ 
self=this; 
typePlantModels.find({namePlant:self.namePlant},function(err,docs){ 
console.log(docs)} 
} 
module.exports=typePlant; 

而且我可以把它放在保存功能或使用save()。然後。請幫我做到這一點。 感謝很多

+0

閱讀要添加自定義的方法,你應該使用貓鼬的方式是打電話之前mongoose.model添加typePlantSchema.methods.findByName = function(){//這個關鍵字是模型,所以你可以在這裏調用this.find(..)}然後簡單地導出模型exports.typePlant = mongoose.model('typePlant',typePlantSchema); – Molda

回答

2

find()功能運行前save()完成。

解決方法是使用save()的回調函數。

var mongoose = require("mongoose"); 
var Schema = mongoose.Schema; 

var typePlantSchema = new Schema({ 
    namePlant:String, 
    estimateDuration:Number, 
    conditionPh:Number 

});  

var typePlantModels= mongoose.model('typePlant',typePlantSchema); 
var typePlantModel = new typePlantModels(); 

mongoose.connect('mongodb://localhost/AeroDB', function (err) { 
    if(err) { 
     console.log('connection error', err); 
    } else { 
     console.log('connection successful'); 
    } 
}); 

typePlantModel.namePlant='tam'; 
typePlantModel.estimateDuration=10; 
typePlantModel.conditionPh=7; 

typePlantModel.save().then(function(){ 
    typePlantModels.find({namePlant:"tam"},function(err,docs){ 
    if(err){ 
     console.log(err); 
    } 
    else {console.log(docs);} 
    }); 
}); 

讀了起來:.save() | Mongoose Documentation


更新1:

爲了使功能外,而不是在回調使得匿名函數,外面聲明函數:

typePlantModel.save().then(findThis("something")); 

function findThis(val){ 
    typePlantModels.find({namePlant:val},function(err,docs){ 
    if(err){ 
     console.log(err); 
    } 
    else {console.log(docs);} 
    }); 
} 

現在你可以只要你想使用findThis()

+0

我知道這種方式。但我不想這樣做。我的邏輯是保存後檢查。可能是我做了一些代碼並找出答案。所以我想找到保存的外觀。實際上,我嘗試編寫數據庫API,並且有人可以調用find函數來查找他們想要的內容。但我的查找功能一直保存後執行。在這裏,你可以寫save(),然後 –

+0

@ user3758586你需要知道'.save()'是一個異步函數。 :) –

+0

請再看看我的更新。 –

0

由於節點是異步的,因此您需要在保存的回調中使用find。像這樣:

typePlantModel.save(function(){ 
    typePlantModels.find({namePlant:"tam"},function(err,docs){ 

    if(err){ 
     console.log(err); 
    } else { 
     console.log(docs); 
    } 
}); 

爲了使代碼更清潔,我建議你在promises

+0

我知道那種方式。但我正在嘗試編寫一個數據庫API。任何一個人只要按照一定的標準打我的發現。他們不在乎內部發生了什麼,他們不知道什麼是保存,回調或任何東西。他們只是叫我的查找功能,我的保存功能,以及它必須做的每件事情。 –