9
9
9karamba2018-12-03 12:23:36
JavaScript
9karamba, 2018-12-03 12:23:36

Why is some data lost when getting data from mongodb?

The function of adding photos and comments, it works well and everything appears in the database, but
loadDatabase.js

The code:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/cs142project6');

//        ....

var allPromises = Promise.all(userPromises).then(function () {
        var photoModels = [];
        var userIDs = Object.keys(mapFakeId2RealId);
        for (var i = 0; i < userIDs.length; i++) {
            photoModels = photoModels.concat(cs142models.photoOfUserModel(userIDs[i]));
        }
        var photoPromises = photoModels.map(function (photo) {
            return Photo.create({
                file_name: photo.file_name,
                date_time: photo.date_time,
                user_id: mapFakeId2RealId[photo.user_id]
            }, function (err, photoObj) {
                if (err) {
                    console.error('Error create user', err);
                } else {

                    photo.objectID = photoObj._id;
                    if (photo.comments) {
                        photo.comments.forEach(function (comment) {
                            photoObj.comments.push({
                                comment: comment.comment,
                                date_time: comment.date_time,
                                user_id: comment.user.objectID
                            });
                            console.log("Adding comment of length %d by user %s to photo %s",
                                comment.comment.length,
                                comment.user.objectID,
                                photo.file_name);
                        });
                    }
                    photoObj.save();
                    console.log('Adding photo:', photo.file_name, ' of user ID ', photoObj.user_id);
                    
                }
            });
        });

Result:
5c04f502d314d605878228.png

When you receive comments on the photo disappear. I can't find where the error is.
webServer.js
The code:
var mongoose = require('mongoose');
var async = require('async');
mongoose.connect('mongodb://localhost/cs142project6');

//         ....

app.get('/photosOfUser/:id', function (request, response) {
       if (!request.session ||!request.session.login_name) {
        response.status(401).send();
        return;
    }
    var user_id = request.params.id;
    Photo.find({user_id: user_id}, function (err, photos) {
        if (photos === undefined) {
           response.status(400).send('Bad param ' + user_id);
            return;
        }
        if (err) {
            console.error('Doing /photosOfUser/' + user_id + 'error:', err);
            response.status(400).send(JSON.stringify(err));
            return;
        }

        var Photo = JSON.parse(JSON.stringify(photos));
        async.each(Photo, function (photo, doneCallback) {
            
        
            delete photo.__v;
            async.each(photo.comments, function (comment, comment_callback) {
                var temp_user_id = comment.user_id;
                console.log("user id "+temp_user_id);
                delete comment.user_id;
               User.findOne({user_id: temp_user_id}, function(err, user) {
                    if (err) {
                        comment_callback(err);
                        return;
                    }
                    if (user.length === 0) {
                        response.status(400).send('Missing user id');
                        return;
                    }
                    comment.user = JSON.parse(JSON.stringify(user));
                    console.log("user "+user);
                    delete comment.user.occupation;
                    delete comment.user.description;
                    delete comment.user.location;
                    delete comment.user.__v;
                    delete comment.user.password;
                    delete comment.user.login_name;
                   delete comment.user.salt;
                   delete comment.user.password_digest;

                    comment_callback();
                });
            }, function (err) {
                doneCallback(err);
            });
        }, function (err) {
            if (err) {
                response.status(400).send(JSON.stringify(err));
            }
            response.end(JSON.stringify(Photo));
        });
    });
});

Result:
5c04f5d8e8c83530217201.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kappy, 2018-12-03
@KappyJS

Because coments comes in an array, parse the array and you'll be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question