Javascript 如何在meteor upsert语句中使用null或“”?
这可能是一个简单的javascript问题。我终于让这个meteor upsert语句起作用了,除了在不存在匹配记录的情况下。如果我将chan.\u id替换为或null,它会起作用,因此我只想在chan找不到现有记录的情况下使用null替换chan.\u id。我就是不知道怎么写这样的东西Javascript 如何在meteor upsert语句中使用null或“”?,javascript,mongodb,null,meteor,upsert,Javascript,Mongodb,Null,Meteor,Upsert,这可能是一个简单的javascript问题。我终于让这个meteor upsert语句起作用了,除了在不存在匹配记录的情况下。如果我将chan.\u id替换为或null,它会起作用,因此我只想在chan找不到现有记录的情况下使用null替换chan.\u id。我就是不知道怎么写这样的东西 //client var chfield = t.find('#today-channel').value, chan = Today.findOne({channel: chfield}) Meteor
//client
var chfield = t.find('#today-channel').value,
chan = Today.findOne({channel: chfield})
Meteor.call('todayUpsert', chan._id, {
channel: chfield,
admin: Meteor.userId(),
date: new Date(),
});
//client and server
Meteor.methods({
todayUpsert: function(id, doc){
Today.upsert(id, doc);
}
});
使用upsert时,除非条目已经存在,否则无法知道_id。在这种情况下,如果您使用文档而不是_id搜索db条目,您应该会得到所需的内容
//client
var chfield = t.find('#today-channel').value;
Meteor.call('todayUpsert', {
channel: chfield,
admin: Meteor.userId(),
date: new Date(),
});
//client and server
Meteor.methods({
todayUpsert: function(doc){
// upsert the document -- selecting only on 'channel' and 'admin' fields.
Today.upsert(_.omit(doc, 'date'), doc);
}
});
我找到了我要找的东西
var chfield = t.find('#today-channel').value,
Meteor.call('todayUpsert',
Today.findOne({channel: chfield}, function(err, result){
if (result) {
return result._id;
}
if (!result) {
return null;
}
}),
{
channel: chfield,
admin: Meteor.userId(),
date: new Date()
}
);
但由于删除了选择器,它的唯一用途变成插入新文档。您如何修改它,以便如果文档存在,它将更新,如果没有,它将创建一个新文档?它总是插入,因为每次调用函数时,日期都会不同。我将编辑上面的代码。