javascript - Push new field and value to json array object in node.js - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750448a4591562.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信