javascript return 语句 returning 未定义

javascript return statement returning undefined

我正在使用量角器测试 angular js salesforce 应用程序。我需要使用 SOQL 和 jsforce 查询 ID,但是当我从另一个 class 调用查询方法时,return 结果未定义。当我在方法中打印出日志时,它确实显示了我正在寻找的 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 的回答后,下面是我收到的错误

Failed: Helper.querySF is not a function TypeError: Helper.querySF is not a function at Object.it (C:\LimService\LSApp\tests\specs\bookingEvents\EditBookingEventTest.js:23:12) at C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:112:25 at new ManagedPromise (C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1067:7) at ControlFlow.promise (C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2396:12) at schedulerExecute (C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:95:18) at TaskQueue.execute_ (C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2970:14) at TaskQueue.executeNext_ (C:\Users\nphillips\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2953:27) at asyncRun (C:\Users\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\selenium-webdriver\lib\promise.js:676:7

如评论中所述,您忘记了一些 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);
        });
}

This seems to work however it returns a promise.

这就是异步代码的意义所在,当函数 returns 时,你想要的结果不存在 (yet);所以你必须处理将来某个时候可用的值。

一种方法是返回 Promise。

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

It looks as though there are 3 promises being returned, or 3 values at least, the 3rd value is the value i need.

不,这只是一个 Promise 链。只能返回单个值,Promise 也只能解析为单个值; 尽管该单个值可能是一个数组

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

I'm not sure how to access that value.

像这样:

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