javascript - Meteor:从服务器方法成功回调后写入数据库

标签 javascript meteor

当我从客户端调用它时,我只想在从服务器成功收到回调时写入数据库。

    Meteor.call('job', 'new', name, script, function(err,response) {
        if(err) {
            console.log(err);
            alert('Error while processing your script. Please make sure syntax is correct.')
            return;
        }else{
            taskid = response;
            console.log(taskid);
            FileSystem.update({ _id: this.params.fileId }, { $set: { content: content, taskid:taskid} }, function (e, t) {
                if (e) {
//error
                    }
                });
            }
        });

现在写它说

Exception in delivering result of invoking 'job': TypeError: Cannot read property 'fileId' of undefined

我预计它只会在服务器调用成功时更新数据库。我怎样才能做到这一点?

最佳答案

假设 this.params 存在,您可能会丢失这些回调函数内的数据上下文。您想要做的是在 Meteor.call() 之前定义一个变量,并将该变量设置为 this.params.fileId。然后您可以在回调函数内使用该变量。

我已在下面的代码中展示了这一点。

var fileId = this.params.fileId;
Meteor.call('job', 'new', name, script, function(err,response) {
        if(err) {
            console.log(err);
            alert('Error while processing your script. Please make sure syntax is correct.')
            return;
        }else{
            taskid = response;
            console.log(taskid);
            FileSystem.update({ _id: fileId }, { $set: { content: content, taskid:taskid} }, function (e, t) {
                if (e) {
//error
                    }
                });
            }
        });

关于javascript - Meteor:从服务器方法成功回调后写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32801694/

相关文章:

javascript - 使用 Iron-router 从 Meteor 生成独立的 SVG

javascript - 如何使用 getElementsByName 访问复选框及其值

javascript - 在我的自定义 wordpress 模板中进行两级切换

mongodb - 从 Collection 中读取 Uint8Array 缓冲区并下载为 pdf

node.js - 将 npm .scss 导入 Meteor App

javascript - 提交后 meteor 表单文本框不清除

Meteor 1.4版本安装包错误

javascript - 在 JavaScript 中为 for 循环设置动画( Canvas )

javascript - 如何从文件系统访问 api window.showDirectoryPicker() 获取选定的目录路径

javascript - 在 jQuery/JS 中使用数组定位重复类中的特定元素