【问题标题】:Meteor call to api resulted in double entriesMeteor 调用 api 导致重复输入
【发布时间】:2017-10-04 16:46:30
【问题描述】:

我使用 Loopback 作为后端 api 服务器到我的数据库,使用 Meteor 作为前端。在 Meteor 中,我使用 Axios 来调用 api。

我想使用 Meteor.call 以便我可以在同一个文件中管理所有相关的 api 调用函数,以便于管理,例如invoice 获取、添加、编辑、删除所有发票文件等

如果我在表单提交时直接调用 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": {}
}

【问题讨论】:

  • API 代码在哪里?
  • 我没有写任何API代码,因为它是从Loopback生成的,我只设置了模型,添加了textContext模型。当我使用 express.js 进行测试时,该 api 工作正常,当我使用 Loopback 附带的资源管理器进行测试时,它也正常工作。
  • 当我在表单提交期间直接使用来自 Meteor 的 axios 进行 post 请求时,textContext 插入正确地完成了 1 条记录,但不是通过 Meteor 调用相同的 post 请求完成的,它生成2条记录。我认为这不是 API 的问题。
  • 你从哪里调用方法?助手还是事件?

标签: meteor


【解决方案1】:

终于找到问题的根源了,因为是在客户端,所以需要用(Meteor.isServer)括起来。

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多