这是我目前的代码:
我希望的是,在每个成功的命令执行完之后(即
我应该在哪里再次调用read()?如果我把它放在
或者我可以为
这是我的代码:
var databaseUrl = "mydb";
var collections = ["users", "reports"];
var db = require("mongojs").connect(databaseUrl, collections);
newuser = {
email: "john@example.com",
password: "iLoveMongo",
sex: "male"
};
saveuser = function(user, callback) {
db.users.save(
user,
function( error, saved) {
if( error || !saved )
console.log( "User not saved");
else
console.log( "User saved" );
callback();
}
);
};
removeuser = function() {
db.users.remove(
{
sex: "male"
},
function( error, removed) {
if( error || !removed )
console.log( "User not deleted");
else
console.log( "User deleted" );
}
);
};
finduser = function() {
db.users.find(
{
sex: "male"
},
function(error, users) {
if( error || !users.length )
console.log( "No male Users found");
else
{
console.log( "male Users found" );
users.forEach(
function(maleUser) {
console.log(maleUser);
}
);
}
}
)
};
updateuser = function() {
db.users.update(
{
email: "john@example.com"
},
{
$set: { password: "iReallyLoveMongo" }
},
function(err, updated) {
if( err || !updated )
console.log("User not updated");
else
console.log("User updated");
}
);
}
var options = {
1: {
option: "Save a User",
execute: saveuser.bind(null, newuser, finduser)
},
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"]();
};
read();
第一次输入命令时会弹出“请输入您的选择”文本,但您仍然可以输入数字并执行其他选项。
这是在控制台上看到的:
PS C:\Users\Benni\Documents\nodejs> node .\mongotest\app.js
Enter your choice:
1
You entered: 1
You choose to do: Save a User
User saved
male Users found
{ _id: 5386ba18463a008011fe6213,
email: 'john@example.com',
password: 'iLoveMongo',
sex: 'male' }
3
You entered: 3
You choose to do: Find a User
male Users found
{ _id: 5386ba18463a008011fe6213,
email: 'john@example.com',
password: 'iLoveMongo',
sex: 'male' }
我希望的是,在每个成功的命令执行完之后(即
options[data]["execute"]()
完成之后),能够再次弹出“输入您的选择”的提示。就像这样:PS C:\Users\Benni\Documents\nodejs> node .\mongotest\app.js
Enter your choice:
1
You entered: 1
You choose to do: Save a User
User saved
male Users found
{ _id: 5386ba18463a008011fe6213,
email: 'john@example.com',
password: 'iLoveMongo',
sex: 'male' }
Enter your choice:
3
You entered: 3
You choose to do: Find a User
male Users found
{ _id: 5386ba18463a008011fe6213,
email: 'john@example.com',
password: 'iLoveMongo',
sex: 'male' }
我应该在哪里再次调用read()?如果我把它放在
choice
的末尾,"输入你的选择"会在options[data]["execute"]();
完成之前弹出。如果我把它放在read
的末尾,我会得到一个无限循环/超过最大调用堆栈大小的错误。或者我可以为
options[data]["execute"]();
添加回调函数吗?类似于:function( null, read){
options[data]["execute"]();
};
?
saveuser()
怎么办呢?我该如何修改它以拥有2个回调函数?我的目标是执行saveuser()
,然后进行finduser()
以查看插入的用户,最后再使用另一个回调函数,即function(){console.log("Enter your choice: ")}
。 - mlessave
的函数,在回调中传回保存的文档,例如callback(null, saved)
。这样你就不需要绑定两个回调参数,就像你所暗示的那样。 - Edwin Dalorzo