MongoDB Stitch returns data as $NumberDouble instead of the number itself

做~自己de王妃 提交于 2021-01-28 05:39:13

问题


I'm using MongoDB Stitch to create a data enabled API, but when I make a GET request, the data is returned where numbers are displayed as:

"firstHit": {
   "$numberInt": "3"

Where I would like them to be return just as:

"firstHit": 3

I have a lot of objects within objects, and I am inserting the data through the mongo shell, I'm not sure of that is of any importance.

Anyone have any experience with this? Thank you!


回答1:


By default, the result format returned by MongoDB Stitch webhooks is in MongoDB Extended JSON format, or EJSON for short. This is useful to define data types that would otherwise be lost in normal JSON. There are some object types that have no equivalent in JSON, for example ObjectId() and Date().

If you would like to return as a normal JSON, you could set the response object as an example below:

exports = function(payload, response) {

    result = {"firsthit": 10};

    response.setStatusCode(200);
    response.setHeader("Content-Type", "application/json");
    response.setBody(JSON.stringify(result));
}

You may also find EJSON library and Stitch Utility Packages as useful additional information.



来源:https://stackoverflow.com/questions/61922685/mongodb-stitch-returns-data-as-numberdouble-instead-of-the-number-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!