我是node.js的新手。我需要在jqgrid中显示Name,但是我只将一个文档的id存储到另一个文档中。
示例
我有2个文件,如学生硕士和学生成绩单。我必须在jqgrid中显示标记详细信息。在标记文档中,我存储了学生ID而不是名称。如何获取名称并将新对象发送到jqgrid?
我的代码如下:
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']总是未定义。但是如果我登录到getStudentName函数,我可以得到getStudentName函数的答案。
我的回调函数只能解决这个问题。如何以简单的方式解决并获得我的结果。请帮助任何人。
答案 0 :(得分:1)
在for
循环
newresult.push({'studentname': getStudentName(results[i].studentid) });
exlpanation:
当您访问newresult[i]
时,它不存在,因此无法访问studentname
字段
答案 1 :(得分:0)
这里的问题是您没有将用户名设置为数组,而是将返回值Function getAllShapes(shp As Shape) As Collection
Dim shapes As Collection
Dim subshp As Shape
Dim colShp As Shape
Dim col As Collection
If shp.Type = 2 Then 'Check if shp is a group
For Each subshp In shp.shapes
shapes.Add (subshp) 'Add the subshp to the shape collection
If subshp.Type = 2 Then 'Check if subshp is group
Set col = getAllShapes (subshp) 'Get all the shapes from the group
For Each colShp In col 'Error here
shapes.Add (colShp) 'Add all the shapes to the collection
Next colShp
End If
Next subshp
Else: shapes.Add (shp)
End If
Set getAllShapes = shapes
Set shapes = Nothing
End Function
设置为数组,因为这是异步方法。也许试试这个东西
student.findOne
我希望对您有帮助