Javascript meteor服务器无需登录即可插入数据
我想创建允许其他应用程序插入(创建)新数据的API。但到目前为止,我尝试了,这与错误消息“用户id是必需的”不起作用,我知道会发生这种情况,因为插入新数据时找不到登录用户。如果使用accounts password的软件包或任何可能的方法,是否可以在不登录的情况下插入新数据,或者以任何可能的方式从服务器端登录 服务器上的代码Javascript meteor服务器无需登录即可插入数据,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我想创建允许其他应用程序插入(创建)新数据的API。但到目前为止,我尝试了,这与错误消息“用户id是必需的”不起作用,我知道会发生这种情况,因为插入新数据时找不到登录用户。如果使用accounts password的软件包或任何可能的方法,是否可以在不登录的情况下插入新数据,或者以任何可能的方式从服务器端登录 服务器上的代码 Picker.route('/createFood/:title', function(params, req, res, next) { console.log('-&g
Picker.route('/createFood/:title', function(params, req, res, next) {
console.log('-> params : ',params);
let username = (new Date()).getTime().toString();
function createFood() {
Fiber(function() {
console.log("-> username : ",username);
let acc = Accounts.createUser({
username: username,
email: username +'@foodie.com',
password: username
});
if (acc) {
console.log("-> acc : ",acc);
// Meteor.call("setUserId", acc);
Menus.insert({
title: params.title,
}, function(err, foodId) {
console.log("-> abs : ",Meteor.absoluteUrl());
console.log("-> err : ",err.message);
console.log("-> foodId : ",foodId);
let code, result;
if (err) {
code = 500;
result = {
error: err.message
}
} else {
code = 200;
result = {
foodId: foodId,
}
}
res.setHeader( 'Content-Type', 'application/json' );
res.statusCode = code;
res.end( JSON.stringify( result ) );
})
}
}).run();
}
if (params.title)
createFood();
});
代码食品模型,这里有用户ID所有者
if (Meteor.isServer) {
Menus.allow({
insert: function() {
return true;
},
update: function() {
return true;
},
remove: function() {
return true;
},
fetch: ['foodId'],
});
Menus.after.insert((userId, doc) => {
....
})
}
没有理由不登录就不能插入数据库。如果你不想,你甚至不需要包括accounts软件包 除非有用户在场,否则您当前的代码不会插入,您可以将其简化为
function createFood() {
Menus.insert({
title: params.title,
没有理由不登录就不能插入数据库。如果你不想,你甚至不需要包括accounts软件包 除非有用户在场,否则您当前的代码不会插入,您可以将其简化为
function createFood() {
Menus.insert({
title: params.title,
感谢回复,但这不是我要找的解决方案:)我最近发现autoValue上的用户ID是主要问题感谢回复,但这不是我要找的解决方案:)我最近发现autoValue上的用户ID是主要问题嘿,我在同一个问题上,有解决方案吗?@Gobliins实际上我的问题在于autoValue用户ID(如果没有用户登录,则此值将为null,如果在其架构文件上使用条件,如
userId?userId:“Ghost user”
,则此时我的临时解决方案将为null)。也许您可以尝试确保在isInsert或isSet时,用户ID没有用作自动值或必填字段。希望这是帮助信息。好吧,那么我的问题就有点不同了。它来自收集钩子,其中包含用户ID(来自Meteor)是未定义的。嘿,我是在同一个问题上,有解决方案吗?@Gobliins实际上我的问题在于autoValue用户ID(如果没有用户登录,此值将为空,如果在其架构文件上使用条件,则我的临时解决方案,如userId?userId:“Ghost user”
)。也许您可以尝试确保在isInsert或isSet时,用户ID没有用作自动值或必填字段。希望这是帮助信息。好吧,那么我的问题就有点不同了。它来自CollectionHooks,其中未定义用户ID(来自Meteor)。