我将后端api服务器的Loopback用于我的数据库,而Meteor用于前端。在Meteor中,我使用Axios来调用api。
我想使用Meteor.call,以便我可以在同一文件中管理所有相关的api调用函数,以便于管理,例如发票在发票文件中获取,添加,编辑,删除等等。
如果我要调用Axios直接在表单提交上发布到API,我的数据库中的记录只显示插入的一个记录(这是预期的)。但是如果我要在Meteor.call中调用Axios,它将导致数据库具有相同上下文的双重条目。
我不太明白双重录入的原因或来源。有人可以解释一下吗?
感谢。
我的Meteor方法调用api来插入一个新的textContext:
Meteor.methods({
'textContext.insert': function(myText, token) {
new SimpleSchema({
myText: {
type: String,
min: 1
},
token: {
type: String,
label: 'Authorization',
min: 1
}
}).validate({myText, token});
const jsonToken = JSON.parse(token);
const url = `http://localhost:3001/v1/users/${jsonToken.userId}/textContexts`;
const data = {
text_context: myText,
user_id: jsonToken.userId
};
return axios({
method: 'post',
url,
headers: {'Authorization': jsonToken.token},
data
})
.then(res => {
console.log(res.data);
return res.data;
})
.catch((err) => {
if (err instanceof Error) {
console.log('Error:', err.message);
}
});
}
});
Loopback中的TextContext模型,text-context.json,从textContextBasic扩展而来,只包含created_at:
{
"name": "textContext",
"base": "textContextBasic",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"text_context": {
"type": "string"
}
},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "user_id"
}
},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"property": "create"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"property": "createOrReplace"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "findById"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "findOne"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "find"
}
],
"methods": {}
}
Loopback中的User.json模型:
{
"name": "user",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"created_at": {
"type": "date",
"required": true,
"default": "$now"
},
"updated_at": {
"type": "date",
"required": true,
"default": "$now"
}
},
"validations": [],
"relations": {
"textContexts": {
"type": "hasMany",
"model": "textContext",
"foreignKey": "user_id"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "__get__textContext"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "__create__textContext"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "__findById__textContext"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "__updateById__textContext"
}
],
"methods": {}
}
答案 0 :(得分:0)
最后找到问题的根源,它需要包含在(Meteor.isServer)
中,因为它在客户端。