gpt4 book ai didi

javascript return 语句返回未定义

转载 作者:行者123 更新时间:2023-12-01 03:37:43 27 4
gpt4 key购买 nike

我正在使用 Protractor 测试 Angular JS salesforce 应用程序。我需要使用 SOQL 和 jsforce 查询 ids,但是当我从另一个类调用查询方法时,返回结果是未定义的。当我在方法中打印日志时,它确实显示了我正在寻找的 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 语句。

除此之外,不要混合使用 promise 和回调。特别是不要使用两者来处理相同的结果。

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.

这就是异步代码的要点,当函数返回时,您想要的结果还不存在;(还)所以你必须处理一个在未来某个时刻可用的值。

实现此目的的一种方法是返回 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);
});

关于javascript return 语句返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com