javascript 将新字段和值推送到 node.js 中的 json 数组对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21238043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push new field and value to json array object in node.js
提问by Vinoth Kumar
I'm new to node.js. I need to display Name in jqgrid, but I stored only id of one document into another document.
我是 node.js 的新手。我需要在 jqgrid 中显示 Name,但我只将一个文档的 id 存储到另一个文档中。
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?
我有 2 个文件,例如学生硕士和学生标记文件。我必须在 jqgrid 中显示标记详细信息。在标记文档中,我存储了学生 ID 而不是姓名。如何获取名称并将新对象发送到 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.
newresult[i]['studentname'] 总是未定义。但是如果我登录 getStudentName 函数,我可以得到 getStudentName 函数的答案。
My callback function is only getting this problem. How to resolve and get my result in an easy way. Please help any one.
我的回调函数只是遇到了这个问题。如何以简单的方式解决并获得我的结果。请帮助任何人。
回答by dark_ruby
try this inside your for
loop
在你的for
循环中试试这个
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
说明:当你访问newresult[i]
它的时候它不存在,所以访问studentname
它的字段是不可能的
回答by Dario García moya
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
您的问题是您没有将用户名设置到数组中,而是将 的返回值设置为student.findOne
,因为这是一个异步方法。也许试试这个
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
我希望它有帮助