gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:45:42 25 4
gpt4 key购买 nike

我有一份服务列表,以及完成该服务所需的相关成本和时间。当我将它们保存在数据库中时,对象看起来像这样:

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

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

最佳答案

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com