gpt4 book ai didi

javascript - 从控制台获取输入

转载 作者:行者123 更新时间:2023-12-03 08:15:58 25 4
gpt4 key购买 nike

我有这个代码

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 文件?

最佳答案

您可以使用名为 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' }
};

祝你好运!

关于javascript - 从控制台获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com