我有这个密码
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/clboTest';
// this could have comed from a form from the browser
var objToInsert = {
_id: 2,
price: 20000,
name: stdin,
description: "20 carat gold ring. ",
//schoolID: { name: 'RUC', address: 'Roskilde', country: 'Denmark' }
};
MongoClient.connect(url, function (err, db) {
var collection = db.collection('products');
collection.insert(objToInsert, function (err, result) {
console.log(result);
db.close();
});
});我不想要硬编码的信息(比如价格,名字)。我如何才能在控制台中输入一些新的东西,而不是一次又一次地用新的信息硬核我的insert.js文件呢?
发布于 2015-11-26 15:51:53
您可以使用一个叫做readline-sync的包。然后,您可以在插入对象之前等待用户输入:
var readlineSync = require('readline-sync');
var name = readlineSync.question('name: ');
var price = readlineSync.questionInt('price: ');
console.log(name);
console.log(price);
var objToInsert = {
_id: 2,
price: price,
name: name,
description: "20 carat gold ring. ",
//schoolID: { name: 'RUC', address: 'Roskilde', country: 'Denmark' }
};祝好运!
https://stackoverflow.com/questions/33937195
复制相似问题