javascript - 为什么我的 collection.upsert() 查询没有为集合设置多个参数?
问题描述
我的 Meteor 方法中有一个collection.upsert()
函数。出于某种奇怪的原因,它似乎只更新了第一个参数,而忽略了更新其余参数。有人可以解释为什么会这样吗?
在事件模板下方找到,其中将userId, password, email, names,
参数传递给regenerateApiKey
subject中的 Meteor 方法
/client/main.js
Template.apiKey.events({
'click .regenerate-api-key': function( ){
var userId = Meteor.userId();
var password = Meteor.user().services.google.accessToken;
var email = Meteor.user().services.google.email;
var names = Meteor.user().services.google.name;
alert("Password: " +password);
alert("email: " +email);
alert("email: " +names);
confirmRegeneration = confirm( "Are you sure? This will invalidate your current key!" );
if ( confirmRegeneration ) {
Meteor.call( "regenerateApiKey", userId, password, email, names, function( error, response ) {
if ( error ) {
alert( error.reason, "danger" );
} else {
+response );
alert( "All done! You have a new API key: " +response );
console.log("Response is: " +response);
}
});
}
}
});
上面的事件模板呈现 5 个弹出警报:
POPUP.1Password: ya29.Glz
POPUP.2email: centos.east@gmail.com
POPUP.3email: Centos East
POPUP.4Are you sure? This will invalidate your current key!
我按是
POPUP.5All done! You have a new API key: [object Object]
下面的代码说明Meteor.call( "regenerateApiKey")
了userId, password, email, names
参数。
/server/main.js
Meteor.methods({
regenerateApiKey: function( userId, password, email, names ){
check( userId, Meteor.userId() );
var newKey = Random.hexString( 32 );
var password = password;
var email = email;
var names = names;
console.log("password: " +password);
console.log("email: " +email);
console.log("names: " +names );
console.log("newKey: " +newKey);
try {
var keyId = APIKeys.upsert( { "owner": userId }, {
$set: {
"key": newKey,
"password": password,
"email": email,
"names": names
}
});
return keyId;
} catch(exception) {
console.log("FAILED UPDATE")
return exception;
}
}
});
在终端中,我可以看到上面的代码呈现的内容:
password: ya29.Glz
email: centos.east@gmail.com
names: Cent East
newKey : 337829bb18082690a32f94a3c23b3782
当我APIKeys.find().fetch()
在控制台中查询时,我得到:
key: "337829bb18082690a32f94a3c23b3782"
_id:"PgBmn6zSYiXTbx6tu"
这表明只有newKey
变量是被set
忽略key
的查询设置password
,email
和names
变量。
有人可以解释为什么集合中没有设置(包含) 、 和 变量password
吗email
?names
解决方案
upsert
下面的查询中只有键的原因:
APIKeys.upsert( { "owner": userId }, {
$set: {
"key": newKey,
"password": password,
"email": email,
"names": names
}
});
实际上是错误的。整个查询正在正确执行。错误在于发布配置本身!
发布配置只允许显示键值 fields
!在我的发布配置代码下面找到:
Meteor.publish( 'APIKey', function(){
var user = this.userId;
var data = APIKeys.find( { "owner": user }, {fields:
{
"key": true,
} });
if ( data ) {
return data;
}
return this.ready();
});
为了纠正这个问题,我不得不将我的发布重新配置为以下代码:
Meteor.publish( 'APIKey', function(){
var user = this.userId;
var data = APIKeys.find( { "owner": user }, {fields:
{
"key": true,
"password": true,
"email": true,
"names": true
} });
if ( data ) {
return data;
}
return this.ready();
});
我要感谢线程中的@Ivo 在这个问题上为我指明了正确的方向。
推荐阅读
- arrays - 如何在 vhdl v93 或 v2002 的端口映射中连接二维数组
- ios - Swift 5 我在安装以测试真实设备时的错误
- c# - 使用 MVC 登录后如何路由到仪表板?
- javascript - 使用 QJSEngine 对我的应用程序有安全影响吗?
- solr - solr 词干分析器与复数不匹配
- javascript - Echarts 库中是否有任何函数可以在 angulajs 中获取 SVG?
- json - 在 Angular Ionic 页面中输入 JSON 数据
- wordpress - 如何在wordpress中获取当前页面名称
- python - 添加自定义 sphinx 扩展,如 scipy.minimize
- javascript - Javascript 中的 UnhandledPromiseRejectionWarning 错误