使用 meteor.js从 MonoDB 检索数字数组
Retrieving an array of numbers from MonoDB with meteor.js
如何使用meteor.js正确地从MongoDB集合对象中检索数字数组?在代码中,我的alert(temp)
假设输出一个相加的数字,例如 5.95+5.95+5.95 = 17.85,但输出是 0[object Object][object Object][object Object][object Object][object Object][object Object][object Object]
,这意味着我没有正确将对象转换为数字格式。请告诉我如何将对象隐藏成我可以将它们相加的数字。
Tasks = new Mongo.Collection("tasks");
if (Meteor.isClient) {
Template.price6.events({
'click a': function () { // in my page, i clicked this multiple times to insert 3 time 5.95 into the Mongodb object.
Tasks.insert({
text: 5.95,
createdAt: new Date() // current time
});
}
});
Meteor.methods({
GetTotal: function () {
var postsArray = Tasks.find().fetch(); // it will fetch the numbers into an array according to the meteor.js doc
var temp = 0.00;
for (index = 0; index < postsArray.length; index++) {
temp += postsArray[index];
}
alert(temp);//suppose to be a number but the output result is weird 0[object][object].....
},
});
}
事件处理程序插入到如下所示的Tasks
集合对象中:
{
text: 5.95,
createdAt: new Date() // current time
}
当您检索记录时,"text"(如果您仅在该属性中存储数字,则这是一个误导性名称)将是 Tasks.find().fetch()
数组的每个元素的键,因此请向代码添加.text
:
for (index = 0; index < postsArray.length; index++) {
temp += parseFloat(postsArray[index].text);
}
相关文章:
- 使用JSP从服务器检索和显示图像
- 通过Magento的网络服务检索运费
- 更改json/javascript对象var名称或从数字var中检索值
- jQuery - 对使用 .text() 检索的数字使用 .split() 会增加很多空项
- 从localStorage检索数字数据
- 从谷歌 v3 API 响应中检索前两位数字
- Javascript:如何检索*字符串*数字的小数位数
- 仅从 ID 字段中检索数字
- 如何使用正则表达式检索字符串中的两个数字
- 如何正确地将JQuery函数从表单的输入标记中检索到的这些字符串对象转换为数字
- 如何检索输入类型'数字'在html中
- Regex:检索重复字符串后的数字
- 无法检索输入数字值
- Jquery检索数字总是在英文格式的谷歌地图api
- Twitter数字——用AngularJS检索用户账户数据
- 使用.replace()在HTML5数据属性上存储jQuery $.data()检索到的数字时未捕获的TypeError
- 从字符串中检索数字的正则表达式
- 从查询字符串中检索数字的最有效方法
- 使用javascript和jquery四舍五入从html检索到的数字
- 匹配字符串部分并检索非邻近数字正则表达式