Articles of javascript

错误的基于名称属性的对象数组

我有一个包含状态和名称属性的对象数组。 我想要使​​用他们的name属性使用下面的代码: array = array.sort(function(a,b){ return a.name – b.name }); 但是它不会返回正确的sorting数组。 这里有一个片段,你可以看到它的行动: var array = [{"name":"whoopsie","status":"active"},{"name":"acpid","status":"active"},{"name":"apache2","status":"active"},{"name":"apparmor","status":"active"},{"name":"apport","status":"active"},{"name":"avahi-daemon","status":"active"},{"name":"bluetooth","status":"active"},{"name":"cron","status":"active"},{"name":"cups","status":"active"},{"name":"cups-browsed","status":"active"},{"name":"dbus","status":"active"},{"name":"grub-common","status":"active"},{"name":"irqbalance","status":"active"},{"name":"kmod","status":"active"},{"name":"lightdm","status":"active"},{"name":"lvm2-lvmetad","status":"active"},{"name":"lvm2-lvmpolld","status":"active"},{"name":"apache-htcacheclean","status":"active"},{"name":"network-manager","status":"active"},{"name":"networking","status":"active"},{"name":"postfix","status":"active"},{"name":"procps","status":"active"},{"name":"resolvconf","status":"active"},{"name":"rsyslog","status":"active"},{"name":"smartmontools","status":"active"},{"name":"smbd","status":"active"},{"name":"speech-dispatcher","status":"active"},{"name":"ssh","status":"active"},{"name":"thermald","status":"active"},{"name":"tlp","status":"active"},{"name":"udev","status":"active"},{"name":"ufw","status":"active"},{"name":"lvm2-lvmpolld","status":"inactive"},{"name":"mysql","status":"active"},{"name":"alsa-utils","status":"inactive"},{"name":"x11-common","status":"inactive"},{"name":"apache-htcacheclean","status":"inactive"},{"name":"avahi-daemon","status":"inactive"},{"name":"console-setup.sh","status":"inactive"},{"name":"cryptdisks","status":"inactive"},{"name":"cryptdisks-early","status":"inactive"},{"name":"cups-browsed","status":"inactive"},{"name":"grub-common","status":"inactive"},{"name":"hwclock.sh","status":"inactive"},{"name":"kerneloops","status":"inactive"},{"name":"keyboard-setup.sh","status":"inactive"},{"name":"laptop-mode","status":"inactive"},{"name":"lvm2","status":"inactive"},{"name":"lvm2-lvmetad","status":"inactive"},{"name":"anacron","status":"inactive"},{"name":"network-manager","status":"inactive"},{"name":"nmbd","status":"inactive"},{"name":"plymouth","status":"inactive"},{"name":"plymouth-log","status":"inactive"},{"name":"pppd-dns","status":"inactive"},{"name":"prometheus","status":"inactive"},{"name":"rsync","status":"inactive"},{"name":"samba","status":"inactive"},{"name":"samba-ad-dc","status":"inactive"},{"name":"saned","status":"inactive"},{"name":"speech-dispatcher","status":"inactive"},{"name":"unattended-upgrades","status":"inactive"},{"name":"uuidd","status":"inactive"},{"name":"unattended-upgrades","status":"active"}]; array = array.sort(function (a,b) { return a.name – b.name }) console.log(array.map(function(i){ return i.name })) 这是输出: 我在nodejs中运行这个代码 [ 'whoopsie', 'unattended-upgrades', 'apache2', 'apparmor', 'apport', 'avahi-daemon', 'bluetooth', 'cron', 'cups', 'cups-browsed', 'dbus', 'grub-common', 'irqbalance', 'kmod', 'lightdm', 'lvm2-lvmetad', 'lvm2-lvmpolld', 'apache-htcacheclean', 'network-manager', 'networking', 'postfix', 'procps', […]

重复的键值违反了唯一的约束 – 使用KnexJS和Postgresql

我目前正在使用KnexJS框架构build一个应用程序,该框架可以帮助我在开发中编写sqlite3,在生产中编写postgresql(用于Heroku)。 我的主要问题是,我的应用程序在我的机器上正常工作,但只要我上传到heroku它打破。 在Heroku日志中,我收到消息: { error: insert into "contracts" ("contract_desc", "contract_header", "owner_id", "signee_id") values ($1, $2, $3, $4) – duplicate key value violates unique constraint "contracts_pkey" 这使我无法将数据插入到我的数据库中。 我的Knex迁移表的设置是这样的: exports.up = function(knex, Promise) { return knex.schema.createTable('contracts', function (table) { table.increments('id').primary() table.integer('owner_id') table.integer('signee_id') table.string('contract_header') table.text('contract_desc') table.string('signature_url') table.string('date_signed') table.boolean('isSigned') }) }; exports.down = function(knex, Promise) { return knex.schema.dropTable('contracts') }; […]

我如何从全局函数声明一个数组?

我想从全局函数声明一个数组,以便我的其他函数也可以使用它,但我不知道如何,因为我使用了一个csvtojson转换器,使整个事情很长,并想知道如果这是申报还是不申请? JS: //require the csvtojson converter class var Converter = require("csvtojson").Converter; // create a new converter object var converter = new Converter({}); var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/myproject'; // call the fromFile function which takes in the path to your // csv file as well as a callback function var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security- […]

Knex从多个表中select

我想用knex运行下面的SQL: select * from ( (select * from foo) union all (select * from bar)) as biz limit 10 offset 20; 有没有办法做到这一点没有knex.raw ?

如何在node.js中创build对象的数组

我的课堂是这样的: var Coin = { _id: null, createGame: function(id) { this._id = id; }, start: function() { } }; 我试图创build一个对象的数组,但我只能创build一个。 有人能告诉我做错了什么吗? 我创build这样的数组: CoinArray = []; CoinArray['123'] = new Coin.createGame(123); CoinArray['333'] = new Coin.createGame(333); CoinArray['333'].start 最后我想要有对象的数组,例如我会采取数组的第一个EL,并执行从硬币类的其他方法。

NodeJS&Express身份validation中间件无法正常运行

我试图运行函数isUserAuthenticated每个请求到服务器通过要求它在app.js和'使用'它是这样的: app.use(authenticate.isUserAuthenticated) 。 我有一个/authenticatecallback路由,由我们的SAML身份提供商POSTED,其中包含validation用户和会话所需的信息。 这是我的authenticate.js文件中的内容: module.exports = router; module.exports.isUserAuthenticated = function(req, res, next) { console.log(req.cookies.subject); if (req.cookies.subject) { console.log(req.cookies.subject) return next(); } res.redirect("LINK TO IDP FOR VERIFICATION, callback func. is then ran to get value of user and session"); } 正如所引用的,这个authentication函数在app.js需要和使用: authenticate = require('./routes/authenticate')和app.use(authenticate.isUserAuthenticated) 。 问题:无论if语句的哪个变体validation请求中是否存在主题cookie,身份validation检查都不会被触发,redirect到IDP身份validation路由也不会被redirect。 上面的代码中的console.log检查正在返回: undefined和{} 。 当我使用isUserAuthenticated函数像这样: router.use('/', isUserAuthenticated, function(req, res, next) […]

无法使用Cheerio获取iframe

您好,我正在试图从wathfree.to获得一个iframe。 这是我用于这个目的的代码: function getSecondBody(url2) { url2 = 'http://www.watchfree.to/watch-366-Forrest-Gump-movie-online-free-putlocker.html'; request(url2, function(err, resp, body) { var $ = cheerio.load(body); var embedcode = $('.links_left_container'); embedcodetext2 = embedcode.html(); console.log(embedcodetext2); return embedcodetext2; }); } 但返回的响应不包含我需要的iframe。 这是我收到的回应: 而实际的页面如下所示: 我的回复中只有iframe部分丢失。

nodejs:mysql无法访问连接查询callback中的全局variables

以下是我的代码 我从客户端JavaScript发送数据对象到这个函数的数据打印它的值 在socket.on中 和 在socket.on – > connection.query(SQL,[],函数(错误,行){数据打印在这里}) 和 在socket.on – > connection.query(SQL,[],函数(错误,行){数据打印在这里}) – > connection.query(SQL,[],函数(错误,行){数据打印未定义}) socket.on('sendmessage', function (data) { var data = JSON.parse(data); console.log(data); if (data.cuserid != '' && users_connected.hasOwnProperty(data.cuserid) && fs.existsSync(users_connected[data.cuserid]['sessionfile'])) { sql = "update hired set echat='Y' where eid=? and jobid=? and jid=? and status in ('given','finished')"; connection.query(sql, [data.cuserid, data.jobid, data.userid], function […]

asynchronousNPM模块不执行数组中的每个项目的setTimeout

我正在使用asynchronousNPM模块并编写了以下程序: var async = require('async'); var arr = [1,2,3,4]; async.each(arr, function (item) { setTimeout(function () { console.log('item: ', item); }, 1000); }, function (error) { console.log('error', error); }); 我期望控制台日志每1秒后显示,但它会在1秒后立即显示所有日志。 我该如何解决?

Mongoose方法返回undefined

我已经成功地定义和使用我的项目mongoose模型: const Schema = mongoose.Schema; const Item = new Schema({ name : String, hierarchy: [{ type: Schema.ObjectId, ref: this }], hierarchy_size: Number }); 在保存之前和之后,我将相应的hierarchy_size和hierarchy值填充到其属性中,使其成为分层树: Item.pre("save", function(next) { this['hierarchy_size'] = this.hierarchy.length; next(); }) Item.post('save', (doc, next) => { if ((doc._id) && (doc.hierarchy.length == 0)) { doc.hierarchy = [doc._id]; doc.save(); } next(); }) 现在我正在为每个根子节点(在查询hierarchy_size == 1之后)运行以下方法getChildren: Item.methods.getChildren […]