简体   繁体   中英

Make a new object from a multiple record parse.com object

I need to make a new object out of a multiple record parse.com object in javascript.

This is how I get the entire fetched object:

 function(articleID, callback) { "use strict"; var Comment = Parse.Object.extend('Comment'); var query = new Parse.Query(Comment); query.descending('createdAt'); query.equalTo('commentFor', {__type:"Pointer", className:"Article", objectId:articleID}); query.equalTo('commentApproved', true); query.find().then(function(results) { callback(results); }, function(error){ callback({error: error}); }); } 

Now, the problem is, that this returns ( callback() ) an object which contains some sensitive data, such as the emails of all commenters, so I want to return a new object with, say, just 'name', 'date', and 'comment'. I just don't know how to build an object like this with a multiple record object as the master.

I am making this in node.js, if that helps in any way.

I'd like something along the line of

[
    {
        "name": "Robert",
        "date": "2016-01-18T17:59:27.378Z",
        "comment": "Hello World!"
    },
    {
        "name": "Bob",
        "date": "2016-01-15T16:37:35.226Z",
        "comment": "Bees knees"
    }
]

I've tried this, but it doesn't seem to work - I don't get the output I'd expect:

 var test = function(articleID, callback) { "use strict"; var Comment = Parse.Object.extend('Comment'); var query = new Parse.Query(Comment); query.descending('createdAt'); query.equalTo('commentFor', {__type:"Pointer", className:"Article", objectId:articleID}); query.equalTo('commentApproved', true); query.find().then(function(results) { var object = {}; for(var i = 0; i < results.length; i++) { object += { commentName: results[i].get('commentName'), commentWebsite: results[i].get('commentWebsite'), commentContent: results[i].get('commentContent'), createdAt: results[i].get('createdAt') }; } callback(object); }, function(error){ callback({error: error}); }); }; 

And then I discovered query.select()

This is a case of RTFM! https://parse.com/docs/js/guide#queries-query-constraints

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM