【问题标题】:convert json array to var values in node.js express.js将 json 数组转换为 node.js express.js 中的 var 值
【发布时间】:2016-07-12 01:34:14
【问题描述】:

我正在尝试将我的 json 数组转换为简单的(编号的)值。 从数据库(.find)中获取它们,问题是它们没有使用 json.parse 进行转换。我是不是做错了什么?

 Voltage.find({},function (err, data) {
        var jsontext = data;
        var parsedData = JSON.parse(jsontext);
        res.json(parsedData);
        console.log(parsedData);
    });

这是会话的 console.log,我只希望:333、333、333 等。

[{"_id":"56f3c19a0298308405d60464","temp":333,"__v":0},{"_id":"56f3c1ee7ec57884068dcb2c","temp":333,"__v":0},{"_id":"56f3c4467ec57884068dcb2d","temp":333,"__v":0},{"_id":"56f3d80191a3c68c138bf04d","temp":337,"__v":0},{"_id":"56f3da3f06cefa781763fb21","temp":337,"__v":0}]

这是我试图仅发送到前端的临时值。我也在使用 mongooose、express.js 和 node.js 以及 mongodb。 感谢观看。

【问题讨论】:

    标签: javascript json node.js mongodb mongoose


    【解决方案1】:

    您可以做的一件事是在查询中取消选择它们:

     Voltage.find().select('-_id -__v').exec(function (err, data) {
            var jsontext = data;
            var parsedData = JSON.parse(jsontext);
            res.json(parsedData);
            console.log(parsedData);
        });
    

    阅读选择方法here

    【讨论】:

    • 太好了,谢谢。这已经缩小了一点,如果 temo 作为 var '333' ,我需要给出值。知道怎么做吗?再次感谢
    • 我在解析 {undefined:1 { temp: 333 },{ temp: 333 },{ temp: 333 },{ temp: 333 },{ temp: 333 } 后返回此消息, { temp: ^ SyntaxError: Unexpected token t at Object.parse (native)
    猜你喜欢
    • 2015-03-05
    • 2018-02-18
    • 2015-08-27
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多