0
我只想檢查數據庫中是否存在密鑰,但是當我嘗試查詢它時,我只能得到NULL。如何在nodejs中用moongoose查詢mongoDB中的數據
這是我invite.js
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://localhost/invitation');
var db2 = mongoose.createConnection;
// User Schema
var InvitationSchema = mongoose.Schema({
key: {
type: String,
index: true
},
used: {
type: String
}
});
var Invitation = module.exports = mongoose.model('Invitation', InvitationSchema);
module.exports.getUsedByKey = function(id, callback){
var query = {used: key};
Invitation.findById(query, callback);
};
module.exports.getInvitationByKey = function(key, callback){
var query = {key: key};
Invitation.findOne(query, callback);
console.log('keythingy ' + callback);
};
這是我嘗試使用功能:
function checkKey(key, res) {
Invitation.getInvitationByKey(key, function(err, key2) {
//console.log('key: ' + key + '\nkey2: ' + key2.key)
if (err) throw err;
if (key2 !=null) {
return key2.key;
} else {
return false;
}
})
}
我還是不明白如何使用這個...假設我已經完成了陳ges,但現在我需要這個「關鍵」,它應該在user.js中返回。我怎麼做? – Emil
對不起。我與你共享一個RestAPI示例意味着你將在請求參數中從客戶端發送關鍵值。 –