简体   繁体   中英

Push new field and value to json array object in node.js

I'm new to node.js. I need to display Name in jqgrid, but I stored only id of one document into another document.

Example

I have 2 documents like Student master and student mark document. I have to display mark details in jqgrid. In mark document I stored student id instead of name. How do I fetch the name and send a new object to jqgrid?

My code is as follows:

exports.getAllstudentsmark = function(req, callback)
{
    studentsmarks.find(function(error, studentsmarks_collection) {
      if( error ) callback(error)
      else {

        studentsmarks_collection.toArray(function(error, results) {
          if( error ) callback(error)
          else {
            newresult = results;
            for(i=0;i<results.length;i++)
            {
                newresult[i]['studentname'] = getStudentName(results[i].studentid);
            }
            console.log(newresult);
            callback(null, newresult)}
        });
      }
    });
}

var getstudentObjectId = function(id)
{
    return student.db.bson_serializer.ObjectID.createFromHexString(id);
}
var getStudentName = function(id)
{
    student.findOne({_id: getstudentObjectId (id)}, function(e, o){
console.log(o.name);
        return o.name;
    });
}

newresult[i]['studentname'] is always getting undefined. But if I log into getStudentName function I can get answer into getStudentName function.

My callback function is only getting this problem. How to resolve and get my result in an easy way. Please help any one.

try this inside your for loop

newresult.push({'studentname': getStudentName(results[i].studentid) });

exlpanation: by the time you access newresult[i] it doesn't exist, so accessing studentname field of it is impossible

Your problem here is that you are not setting the name of the user into the array, but the return value of student.findOne , since this is an asynchronous method. Maybe try this thing

exports.getAllstudentsmark = function(req, callback)
{
  studentsmarks.find(function(error, studentsmarks_collection) {
  if( error ) callback(error)
  else {

    studentsmarks_collection.toArray(function(error, results) {
      if( error ) callback(error)
      else {
        newresult = [];
        for(i=0;i<results.length;i++)
        {
            getStudentName(results[i].studentid, function (studentName) {
               newresult.push({studentname: studentName});
            })
        }
        console.log(newresult);
        callback(null, newresult)}
    });
  }
});
}

var getstudentObjectId = function(id)
{
    return student.db.bson_serializer.ObjectID.createFromHexString(id);
}
var getStudentName = function(id, callback)

{
    student.findOne({_id: getstudentObjectId (id)}, function(e, o){
console.log(o.name);
        callback(o.name);
    });
}

I hope it helps

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