Javascript 如何在Meteor JS中存储成本数据,使其可以增加?
我有一份服务清单,列出了完成服务所需的相关成本和时间。当我将它们保存到数据库中时,对象如下所示:Javascript 如何在Meteor JS中存储成本数据,使其可以增加?,javascript,meteor,Javascript,Meteor,我有一份服务清单,列出了完成服务所需的相关成本和时间。当我将它们保存到数据库中时,对象如下所示: cost: "1" name: "Yawn" time: "10" 我相信当我在Meteor方法中调用这些对象时,数字周围的括号不允许我的应用程序增加这些对象 我在插入时不断收到此错误: > errorClass {stack: "Error↵ at m.(anonymous function) (http://localh…7f11e3eaafcbe13d80ab0fb510d25d
cost: "1"
name: "Yawn"
time: "10"
我相信当我在Meteor方法中调用这些对象时,数字周围的括号不允许我的应用程序增加这些对象
我在插入时不断收到此错误:
> errorClass {stack: "Error↵ at m.(anonymous function) (http://localh…7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:3735:17)", error: 409, reason: "MinimongoError: Cannot apply $inc modifier to non-number"
使用此功能添加服务:
Meteor.methods({
createService: function(postAttributes) {
var user = Meteor.user();
// ensure the user is logged in
if (!user)
throw new Meteor.Error(401, "You need to login to post new stories");
// ensure the post has a title
if (!postAttributes.name)
throw new Meteor.Error(422, 'Please fill in a name');
// ensure the service has a cost
if (!postAttributes.cost)
throw new Meteor.Error(422, 'Please fill in a cost');
// ensure the post has a title
if (!postAttributes.time)
throw new Meteor.Error(422, 'Please fill in a time');
console.log(postAttributes);
// pick out the whitelisted keys
var post = _.extend(_.pick(postAttributes, 'name'), {
cost: postAttributes.cost,
time: postAttributes.time,
userId: user._id,
author: user.emails[0].address,
submitted: new Date().getTime()
});
var postId = Services.insert(post);
return postId;
}
});
Appointments.update(postAttributes.appointmentId, { $inc :
{ "appointmentTotal": service.cost} } );
我使用此函数递增:
Meteor.methods({
createService: function(postAttributes) {
var user = Meteor.user();
// ensure the user is logged in
if (!user)
throw new Meteor.Error(401, "You need to login to post new stories");
// ensure the post has a title
if (!postAttributes.name)
throw new Meteor.Error(422, 'Please fill in a name');
// ensure the service has a cost
if (!postAttributes.cost)
throw new Meteor.Error(422, 'Please fill in a cost');
// ensure the post has a title
if (!postAttributes.time)
throw new Meteor.Error(422, 'Please fill in a time');
console.log(postAttributes);
// pick out the whitelisted keys
var post = _.extend(_.pick(postAttributes, 'name'), {
cost: postAttributes.cost,
time: postAttributes.time,
userId: user._id,
author: user.emails[0].address,
submitted: new Date().getTime()
});
var postId = Services.insert(post);
return postId;
}
});
Appointments.update(postAttributes.appointmentId, { $inc :
{ "appointmentTotal": service.cost} } );
我完全无法用数字格式而不是字符串来计算服务的成本和时间。正如您所说,似乎您只需要将字符串转换为整数:
var post=u.extend(u.pick(posattributes,'name'){
成本:parseInt(posttributes.cost),
时间:parseInt(posttributes.time),
userId:user.\u id,
作者:user.emails[0]。地址,
已提交:新日期().getTime()
});