首页 \ 问答 \ nodejs writeFile返回重复项(nodejs writeFile returning duplicates)

nodejs writeFile返回重复项(nodejs writeFile returning duplicates)

我编写了以下nodejs脚本(sortLabels.js),它适用于labelsFilePath的文件,它包含对象,但它不适用于labelIdsFilePath文件,这是一个字符串数组。 我基本上尝试对两个文件进行排序,然后使用新排序的值覆盖这些文件的内容。 出于某种原因,在labelIds文件中,我得到重复。

let labelsFilePath = './server/mocks/label.json';
let labels = require(labelsFilePath);
let labelIdsFilePath = './client/app/common/config/label-ids.json';
let labelIds = require(labelIdsFilePath);
const _ = require('lodash');
let fs = require('fs');

labels = _.sortBy(labels, "id"); 
labelIds = _.sortBy(labelIds, (num) => num);

fs.writeFile(labelsFilePath, JSON.stringify(labels), function(err){
  if(err) console.log(err); else {
    console.log('labels were sorted');
  }
})

fs.writeFile(labelIdsFilePath, JSON.stringify(labelIds), function(err){
  if(err) console.log(err);
  else {
    console.log('labels ids were sorted');
  }
})

在运行脚本( node sortLabels )之前,这就是labelIds的样子:

 [
  "0083",
  "0117",
  "0021",...]

之后:

["0012","0012","0021","0021"....]

我做错了什么/失踪了?


I have written the following nodejs script (sortLabels.js), and it works fine for the file at labelsFilePath, which containts object, but it doesn't work for the file at labelIdsFilePath which is an array of strings. I am basically trying to sort both files and then overwrite the contents of those files with the newly sorted values. For some reason, in the labelIds file, I get duplicates.

let labelsFilePath = './server/mocks/label.json';
let labels = require(labelsFilePath);
let labelIdsFilePath = './client/app/common/config/label-ids.json';
let labelIds = require(labelIdsFilePath);
const _ = require('lodash');
let fs = require('fs');

labels = _.sortBy(labels, "id"); 
labelIds = _.sortBy(labelIds, (num) => num);

fs.writeFile(labelsFilePath, JSON.stringify(labels), function(err){
  if(err) console.log(err); else {
    console.log('labels were sorted');
  }
})

fs.writeFile(labelIdsFilePath, JSON.stringify(labelIds), function(err){
  if(err) console.log(err);
  else {
    console.log('labels ids were sorted');
  }
})

before running the script (node sortLabels), this is what labelIds look like:

 [
  "0083",
  "0117",
  "0021",...]

and after:

["0012","0012","0021","0021"....]

what am I doing wrong/missing?


原文:https://stackoverflow.com/questions/39903062
更新时间:2021-09-16 08:09

最满意答案

我认为你正在点击以下FF错误: https//bugzilla.mozilla.org/show_bug.cgi?id = 667001

它已在2年前报道,有多个重复,但截至目前还没有修复。 :(


I think you are hitting the following FF bug: https://bugzilla.mozilla.org/show_bug.cgi?id=676001

It's been reported 2 years ago, has multiple duplicates, but as of now have not been fixed yet. :(

相关问答

更多
  • 一段时间后,我向Chrome开发团队报告了这个问题,因为我认为这是一个错误。 但事实并非如此。 当元素从文档树中移除,然后在mouseup之前重新添加(例如appendChild(lastChild))的情况的行为尚未由W3委员会定义。 因为结果行为因浏览器而异。 问题的进展可以通过以下链接进行追踪: Chrome问题线程 W3C发布讨论主题 After some time I reported this question to Chrome dev team because I thought it is ...
  • 您的页面包含两个名称相同的元素\ id: IMG属性为"zoomIn" IMG HTML元素和名称为zoomIn javascript function 。 尝试重命名function zoomIn或IMG#zoomIn 。 You page contain two elements with same name\id: IMG HTML element with ID attribute "zoomIn" and javascript function with name zoomIn. Try to r ...
  • 目前只有Chrome支持通过CSS设置矩形高度和宽度,因为这是SVG 2的新功能。在所有情况下,您都需要包含宽度和高度作为属性。 两次具有相同的属性名称是无效的,因此您也应该删除冗余类属性。 Only Chrome supports setting rect height and width via CSS currently as that's a new feature of SVG 2. You need to include width and height as attributes in all ...
  • 为什么SVG路径的行为与每个其他HTML元素的行为不同? 两个原因: SVG元素不是HTML元素,并且不受相同规则的约束。 定义的行为与百分比坐标在SVG中的其他位置工作的方式一致。 已经提出了一个名为transform-box新属性,它允许您改变HTML和SVG的transform-origin行为。 如果浏览器支持此新属性,您将能够通过以下方式获得与Chrome当前行为匹配的行为: transform-box: fill; FF已经实现了这一点,但默认情况下它尚未启用(AFAIK)。 现在,您需要自己 ...
  • 好的,所以错误是我自己的。 createObject方法的initParams参数需要是一个字符串,而不是Silverlight插件在Firefox和Safari中正确初始化的对象(见下文)。 IE似乎没有打扰。 Silverlight.createObject( "silverlight/MediaPlayer.xap?verison=16012013", _container, "silverlightControl", params, { onE ...
  • 我不知道为什么FF这个文件有问题。 您可能希望将此报告为错误。 幸运的是,有一个简单的解决方案。 摆脱Illustrator添加的那些有些不必要的面具。
    我认为你正在点击以下FF错误: https : //bugzilla.mozilla.org/show_bug.cgi?id = 667001 它已在2年前报道,有多个重复,但截至目前还没有修复。 :( I think you are hitting the following FF bug: https://bugzilla.mozilla.org/show_bug.cgi?id=676001 It's been reported 2 years ago, has multiple duplicates, ...
  • 尝试这个: 如果applet在准备就绪时调用qzReady,则将useDefaultPrinter放在该函数中。 如果isLoaded需要一些时间,请使用setTimeout调用useDefaultPrinter 喜欢这个