Javascript 使用meteor.js从MonoDB检索数字数组
如何使用meteor.js从MongoDB集合对象中正确检索数字数组? 在代码中,我的Javascript 使用meteor.js从MonoDB检索数字数组,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,如何使用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 object object object][object object object object],这
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 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=newmongo.Collection(“任务”);
if(Meteor.isClient){
Template.6.6事件({
“单击a”:函数(){//在我的页面中,我多次单击该函数以将3 time5.95插入Mongodb对象。
任务.插入({
案文:5.95,
createdAt:new Date()//当前时间
});
}
});
流星法({
GetTotal:函数(){
var postsArray=Tasks.find().fetch();//它将根据meteor.js文档将数字提取到一个数组中
var-temp=0.00;
对于(索引=0;索引
事件处理程序将如下所示插入任务
集合对象:
{
text: 5.95,
createdAt: new Date() // current time
}
当您检索回记录时,“text”(如果您只在该属性中存储数字,这是一个误导性的名称)将是任务的每个元素的键。find().fetch()
数组,因此将.text
添加到您的代码中:
for (index = 0; index < postsArray.length; index++) {
temp += parseFloat(postsArray[index].text);
}
for(index=0;index
The.text东西修复了[Object]东西,现在输出数字,但它是05.955.955.95.95。我怎么能把它们相加而不是排列起来呢?哦,我知道了。我需要使用parseFloat()来转换它。