, , , . , , , , . , , . , , :
newuser = {
email: "john@example.com",
password: "iLoveMongo",
sex: "male"
};
saveuser = function(user, callback) {
db.users.save(
user,
function( error, saved) {
if( error || !saved )
callback( "User not saved");
else
callback(saved);
}
);
};
removeuser = function(callback) {
db.users.remove(
{
sex: "male"
},
function( error, removed) {
if( error || !removed )
callback( "User not deleted");
else
console.log( "User deleted" );
callback();
}
);
};
finduser = function(callback) {
db.users.find(
{
sex: "male"
},
function(error, users) {
if( error || !users.length )
callback( "No male Users found");
else
{
console.log( "male Users found" );
callback(null, users);
}
}
)
};
updateuser = function(callback) {
db.users.update(
{
email: "john@example.com"
},
{
$set: { password: "iReallyLoveMongo" }
},
function(err, updated) {
if( err || !updated )
callback("User not updated");
else
console.log("User updated");
callback(null, updated);
}
);
}
var options = {
1: {
option: "Save a User",
execute: saveuser.bind(null, newuser)
},
2: {
option: "Remove a User",
execute: removeuser
},
3: {
option: "Find a User",
execute: finduser
},
4: {
option: "Update a User",
execute: updateuser
}
}
function read() {
console.log("");
console.log(" Enter your choice: ");
stdin = process.stdin;
stdin.setEncoding('utf8');
stdin.on('data', choice);
};
function choice (data) {
data = parseInt(data);
console.log("You entered: " + data);
console.log("You choose to do: " + options[data]["option"]);
options[data]["execute"](function(err, res){
if(err){
console.log(err);
}
if(res){
if(Array.isArray(res)){
res.forEach(function(item){
console.log(item);
})
} else {
console.log(res);
}
}
console.log(" Enter your choice: ");
});
};
read();
Promises
promises. , , , .
Q Promises , , . , Q .
, , -, . Q.ninvoke, , , .
var Q = require('q');
saveUser = function(user) {
return Q.ninvoke(db.users,"save", user)
.then(function(user){
console.log("User saved: %j", user);
})
.catch(function(error){
console.log("Error while saving: %s", error);
});
};
removeUser = function() {
return Q.ninvoke(db.users,"remove", {sex: "male"})
.then(function(result){
console.log("User removed: %j", result);
})
.catch(function(error){
console.log("Error while removing: %s", error);
});
};
findUser = function() {
return Q.ninvoke(db.users,"find", {sex: "male"})
.then(function(user){
console.log("User found: %j", user);
})
.catch(function(error){
console.log("Error while finding: %s", error);
});
};
updateUser = function() {
return Q.ninvoke(db.users,"update",
{
email: "john@example.com"
},
{
$set: { password: "iReallyLoveMongo" }
})
.then(function(user){
console.log("User updated: %j", user);
})
.catch(function(error){
console.log("Error while updating: %s", error);
});
};
, -, .
, , :
function read() {
console.log("");
console.log(" Enter your choice: ");
stdin = process.stdin;
stdin.setEncoding('utf8');
stdin.on('data', choice);
};
function choice (data) {
data = parseInt(data);
console.log("You entered: " + data);
console.log("You choose to do: " + options[data]["option"]);
options[data]["execute"]().then(function(){
console.log("Enter your choice: ")
});
};
read();
, [data] [ "execute" ] . , " ".
voila:
Enter your choice:
1
You entered: 1
You choose to do: Save a User
User saved: [{"email":"john@example.com","password":"iLoveMongo","sex":"male","_id":"5388f57035a2eda13b7e4ea7"},{"n":0}]
Enter your choice:
2
You entered: 2
You choose to do: Remove a User
User removed: {"n":2}
, .