我讀this並嘗試實現我的功能,以便數據不會變回,但它不適用於我。引用Javascript調用不起作用
我有一個對象數組,我把它們一一發送給另一個函數來添加數據。
queries.first(finalObject.sectionProjects[i]);
爲sectionProjects
中的每一個,有一個可變achievements
,用一個空數組。
在發送每sectionProject到the queries.first
功能,我重新分配的成就,
finalObject.sectionProjects[i].achievements = something else
當我從queries.first函數返回,我輸了,我添加的數據。 我做錯了什麼?
這裏的功能:
module.exports = {
first:function(aProject) {
// Latest achievements
var query =
" SELECT ta.description, ta.remarks, ta.expectedECD " +
" FROM project pr, task ta, milestone mi " +
" WHERE pr.ID = mi.project_ID AND mi.ID = ta.milestone_ID " +
" AND ta.achived = ta.percent AND pr.ID = " + aProject.project_id +
" ORDER BY pr.expectedECD " +
" LIMIT 5;"
;
var stringified = null;
pmdb.getConnection(function(err, connection){
connection.query(query, function(err, rows){
if(err) {
throw err;
}else{
var jsonRows = [];
for(var i in rows) {
stringified = JSON.stringify(rows[i]);
jsonRows.push(JSON.parse(stringified));
}
connection.release();
aProject.achievements = jsonRows;
upcomingTasks(aProject);
}
});
});
}
}
這是pmdb.js:
var mysql = require("mysql");
var con = mysql.createPool({
host: "localhost",
user: "user",
password: "password",
database: "database"
});
module.exports = con;
這是調用queries.first主要功能:
// ...Code...
//Number of section projects
var len = jsonRows.length;
console.log("Number of section projects: " + len);
var internal_counter = 0;
function callbackFun(i){
(finalObject.sectionProjects[i]).achievements = [];
queries.first(finalObject.sectionProjects[i]);
if(++internal_counter === len) {
response.json(finalObject);
}
}
var funcs = [];
for (var i = 0; i < len; i++) {
funcs[i] = callbackFun.bind(this, i);
}
for (var j = 0; j < len; j++) {
funcs[j]();
}
你需要分享'queries.first',它是如何實現的,它是一個異步的等等 –
發佈你的函數分配成就。所以我可以調試。也讓小提琴 – murli2308
你的功能似乎沒問題。小提琴調試你的代碼。或發佈完整的代碼以便更好地理解。 – murli2308