jQuery:使用ajax请求中的所有数据更新数据库
jQuery : update the database with all of the data from the ajax-request
am正在尝试更新数据库。
因为我这样做
来自js代码
var data = {
"jobid": $('#jobid').val(),
"names": $('#names').val(),
"scripttype": $('#testscripts').val()
};
var msg="";
for(i = 1; i <=4; i++) {
data["Param" + i + "Key"] = $('#key' + i).val();
data["Param" + i + "Value"] = $('#value' + i).val();
}
$.ajax({
type: 'post',
url: "/save",
dataType: "json",
data: data
});
在node.js侧
jobsCollection.update({
_id: id
}, {
$set: {
names: record.names,
script: record.scripttype,
// i dont know what should i place in this part???
// i have to set paramkey and paramValues
}
},
{
upsert: true
},
function (err, result) {
if (!err) return context.sendJson([], 404);
});
在record.names和record.scripttype中获取正确的值。
I don't know how to set values got from the for loop for updating
request going
Request: /save
{ jobid: '',
names: 'first',
scripttype: 'fow',
Param1Key: '1',
Param1Value: 'oneeeeee',
Param2Key: '2',
Param2Value: 'twoooooooo'
etc........
............
}
由于属性名称是动态的,因此您需要使用JavaScript的索引器样式的属性访问器,如下所示。
只要基本上扭转这个过程。我不确定数据在您调用update
的点上的位置,所以在下面的示例中我将其称为sourceData
;
// create an object with the well known-property names
var set = {
names : record.names,
script : record.scripttype
};
// now loop through the params and set each one individually
for(i = 1; i <=4; i++) {
var key = "Param" + i + "Key"; // build a key string
set[key] = sourceData[key]; // grab the data and set it
key = "Param" + i + "Value"; // and repeat
set[key] = sourceData[key];
}
然后,将其传递给您的更新:
jobsCollection.update({
_id: id
}, {
$set: set // pass the object created above
}, /* etc. */
如果你不知道Params
的计数,你可以:
- 发送
- 改为使用
for .. in
循环遍历所有属性
对于#2:
for(var key in sourceData) {
// you could filter the property names here if you'd like
// (like if only Params# were important)
set[key] = sourceData[key];
}
相关文章:
- 平均值:无法将数据更新到数据库
- d3在数据更新时错误地附加了dom元素
- 使用角度传递的数据更新模态
- jQuery使用XML数据更新UL列表
- ng表DOM未使用数据更新进行更新
- 在线/离线数据更新移动最佳实践
- 使用 预测 API 数据更新的 HTML 模板
- JQuery 数据表列数据更新工作太慢
- 使用Plotly中的新数据更新图形的高性能方法
- 使用新数据更新 D3 饼图.json
- jQuery - 使用相同的数据更新 2 个或多个表单字段
- 如何在数据更新时更新可重用的 d3.js 图形
- 组合框/下拉框在数据更新后未更新
- 在 JavaScript 中的数据更新后,我的 DOM 对象不会更新
- 如何使用来自外部源的数据更新 ng 模型
- 主干.js:使用不同的数据更新集合
- 对数据库进行 AJAX 编辑时,我是否应该立即使用新数据更新接口
- 计划脚本以使用外部数据更新数据库
- 使用从 MongoDB 获取的新数据更新对象属性
- 如何每 n 分钟使用数据更新一次 Web 应用程序