Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Arrays json将对象转换为数组nodejs_Arrays_Json_Node.js_Routing - Fatal编程技术网

Arrays json将对象转换为数组nodejs

Arrays json将对象转换为数组nodejs,arrays,json,node.js,routing,Arrays,Json,Node.js,Routing,在我的路由中,我用JSON响应如下 app.get('/loginerr', function(req, res, next){ var message = req.flash('signupMessage'); res.json({'error' : message}); }); 消息是一个简单的字符串类型,但JSON对象在如下数组中发送: { "error": [ "The email is already taken" ] } 如您所见,响应中有一对括号表示

在我的路由中,我用JSON响应如下

app.get('/loginerr', function(req, res, next){
    var message = req.flash('signupMessage');
    res.json({'error' : message});
});
消息是一个简单的字符串类型,但JSON对象在如下数组中发送:

{
  "error": [
  "The email is already taken"
  ]
}

如您所见,响应中有一对括号表示数组。如何摆脱它们?

我不知道req.flash对象是一个数组

我只需要取数组中唯一的第一个元素:

res.json({'error' : message[0]});
您可以使用以下选项:

var message = req.flash('signupMessage')[0];
res.json({'error' : message});

消息是一种简单的字符串类型
;不这是一个数组。这就是JSON告诉你的。谢谢你,我刚刚意识到是的,我之前就知道了。非常感谢。