如何在Meteor JS中存储成本数据,以便它可以递增?

时间:2014-04-28 23:27:02

标签: javascript meteor

我有一份服务清单,其中包含相关费用和完成服务所需的时间。当我将它们保存在数据库中时,该对象如下所示:

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} } );

我完全迷失了以数字格式而不是字符串获取服务的成本和时间。

1 个答案:

答案 0 :(得分:1)

好吧,正如你所说,你似乎只需要把你的字符串变成整数:

var post = _.extend(_.pick(postAttributes, 'name'), {
  cost: parseInt(postAttributes.cost),
  time: parseInt(postAttributes.time),
  userId: user._id, 
  author: user.emails[0].address, 
  submitted: new Date().getTime()
});