admin管理员组

文章数量:1391955

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.

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.

Share Improve this question edited Jan 21, 2014 at 10:19 Metalskin 4,2787 gold badges40 silver badges63 bronze badges asked Jan 20, 2014 at 15:38 Vinoth KumarVinoth Kumar 4993 gold badges19 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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

本文标签: javascriptPush new field and value to json array object in nodejsStack Overflow