Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要JSON中不带引号的值_Javascript_Json_Node.js - Fatal编程技术网

Javascript 需要JSON中不带引号的值

Javascript 需要JSON中不带引号的值,javascript,json,node.js,Javascript,Json,Node.js,这是我的代码: router.post('/findmanychallengesbyid', auth, function(req, res, next){ if(!req.body.ids){ return res.status(400).json({message: 'Geen ids'}); } var challengeData = []; for( var i = 0; i < JSON.parse(req.body.ids).length; i++ )

这是我的代码:

router.post('/findmanychallengesbyid', auth, function(req, res, next){
  if(!req.body.ids){
    return res.status(400).json({message: 'Geen ids'});
  }

  var challengeData = [];
  for( var i = 0; i < JSON.parse(req.body.ids).length; i++ ) {
    Challenge.findById(JSON.parse(req.body.ids)[i].toString().replace(/\"/g, ""), function (err, doc){
      challengeData.push(doc);
    });
  }

  res.json(challengeData);

});
Challenge.findById需要将此作为输入:

56313da58c5ba50300f2b4f0

现在它将此作为输入:

“56313da58c5ba50300f2b4f0”

那么我如何才能做到这一点呢?

使用以下方法:

.replace(/\"/g, "")

要替换“

将JSONValue转换为字符串,请执行以下操作:

String.ValueOf(iets);
也可以使用String类的replaceAll函数

iets.toString().replaceAll("\"", "");
此方法替换IET中的所有双引号,而不是第一个和最后一个双引号


示例:“Hello”变为Hello,但如果它是“He”llo,则根据您的要求应该是“He”llo,但它变为Hello。意思是说所有的双引号都被替换了。

如果我正确理解代码,你想搜索一个id数组

正确的方法是,使用

router.post('/findmanychallengesbyid', auth, function(req, res, next){
  if(!req.body.ids){
    return res.status(400).json({message: 'Geen ids'});
  }

  Challenge.find({
    '_id': { $in: JSON.parse(req.body.ids)}
    }, function(err, docs){
      res.json(docs);
  });

});

这个问题看起来像是你在试图解决一个小问题,而这个小问题是一个更大问题的一部分,可能有一种不同的更简单的方法来解决这个更大的问题。关于这个问题,您更大的目标是什么?关于这个问题,
“563140668c5ba50300f2b4f3”
是控制台表示,还是
实际在字符串中?未加引号的
563140668c5ba50300f2b4f3
不是JavaScript中任何内容的有效表示。它必须被视为一个字符串。我已经编辑了op以获得更详细的信息。
req
看起来像什么?它是一个包含未解析字符串的javascript对象?挑战来自哪里?
router.post('/findmanychallengesbyid', auth, function(req, res, next){
  if(!req.body.ids){
    return res.status(400).json({message: 'Geen ids'});
  }

  Challenge.find({
    '_id': { $in: JSON.parse(req.body.ids)}
    }, function(err, docs){
      res.json(docs);
  });

});