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

cost: "1"
name: "Yawn"
time: "10"


我相信在流星方法中调用数字时,数字周围的括号不允许我的应用递增这些对象。

我在插入时不断收到此错误:

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


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

最佳答案

嗯,正如您所说,似乎您只需要将字符串转换为整数即可:

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

关于javascript - 如何将成本数据存储在Meteor JS中以便可以递增?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23352910/

10-12 00:51