首页 文章

返回undefined的javascript return语句

提问于
浏览
0

我正在使用量角器测试角度js salesforce应用程序 . 我需要使用SOQL和jsforce查询id但是当我从另一个类调用查询方法时,返回结果是未定义的 . 当我打印出方法中的日志时,它确实显示我正在寻找的id似乎在return语句中丢失了 .

var jsforce = require('jsforce');

function querySF() {
  var conn = new jsforce.Connection({
    // you can change loginUrl to connect to sandbox or prerelease env.
    loginUrl: 'https://www.salesforce.com'
  });
  conn.login('some username', 'some password', function(err, userInfo) {
    if (err) {
      return console.error(err);
    }
    // Now you can get the access token and instance URL information.
    // Save them to establish connection next time.
    console.log(conn.accessToken);
    console.log(conn.instanceUrl);
    // logged in user property
    console.log("User ID: " + userInfo.id);
    console.log("Org ID: " + userInfo.organizationId);
    // ...
  }).then(function() {
    conn.query("SELECT Id FROM anObject__c Where name = 'the name'", function(err, result) {
      if (err) {
        return console.error(err);
      }
      var records = [];
      console.log("total : " + result.totalSize);
      console.log("fetched : " + result.records.length);
      // is returning the id
      console.log(result.records[0].Id);
      // the class that calls the methods saves it to a variable, the variable is undefined
      return result.records[0].Id;
    });
  });


}

更新我的代码以匹配Thomas的答案后,下面是我收到的错误

失败:Helper.querySF不是函数TypeError:Helper.querySF不是C:\ Users上的Object.it(C:\ LimService \ LSApp \ tests \ specs \ bookingEvents \ EditBookingEventTest.js:23:12)中的函数新的ManagedPromise上的\ n \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:112:25(C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:1067:7)在ControlFlow.promise(C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2396:12)at at在TaskQueue.execute_(C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules)中的schedulerExecute(C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:95:18) \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2970:14)在TaskQueue.executeNext_(C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise .js:2953:27)在asyncRun(C:\ User s \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2860:25)at C:\ Users \ nphillips \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \硒的webdriver \ lib中\ promise.js:676:7

1 回答

  • 2

    正如评论中提到的,你忘了一些 return 语句 .

    除此之外,不要混淆承诺和回调 . 特别是不要同时使用两者来处理相同的结果 .

    var jsforce = require('jsforce');
    
    function querySF() {
        var conn = new jsforce.Connection({
            // you can change loginUrl to connect to sandbox or prerelease env.
            loginUrl: 'https://www.salesforce.com'
        });
    
        return conn.login('some username', 'some password')
            .then(function(userInfo) { 
                // Now you can get the access token and instance URL information.
                // Save them to establish connection next time.
                console.log(conn.accessToken);
                console.log(conn.instanceUrl);
                // logged in user property
                console.log("User ID: " + userInfo.id);
                console.log("Org ID: " + userInfo.organizationId);
                // ...
    
                return conn.query("SELECT Id FROM anObject__c Where name = 'the name'")
            })
            .then(function(result){
                  console.log("total : " + result.totalSize);
                  console.log("fetched : " + result.records.length);
                  // is returning the id
                  console.log(result.records[0].Id);
                  // the class that calls the methods saves it to a variable, the variable is undefined
                  return result.records[0].Id;
            })
            .catch(function(err){
                console.error(err);
            });
    }
    

    这似乎有效,但它会返回一个承诺 .

    当函数返回时,那里还有's the point of async code, that the result you want ain';所以你必须处理将来某个时候可用的 Value .

    一种方法是通过返回Promise .

    也许你应该读这个:How do I return the response from an asynchronous call?

    看起来好像有3个承诺被返回,或者至少有3个值,第3个值是我需要的值 .

    不,这只是一个Promise链 . 只返回一个值,而Promise也只能解析为单个值;虽然该单个值可能是一个数组 .

    你应该习惯Promise . 它们比回调语法更方便 .

    我不确定如何访问该值 .

    像这样:

    querySF().then(function(id){
        console.log("result: ", id);
    });
    

相关问题