Node v9.4.0下Square-connect报".then is not a function"错误,需额外操作吗?
Looks like you're hitting a common quirk with older versions of the Square Connect SDK—let's break this down and get it sorted quickly.
Why this error pops up
Older releases of the square-connect package don’t return Promises by default for their API methods. Instead, they rely on traditional callback functions. So when you try to chain .then() onto api.listLocations, Node throws an error because that method isn’t returning a Promise object at all.
Solutions to try
1. Upgrade to the latest SDK version
The simplest fix is to update your square-connect package to a version that supports Promises out of the box. Run this command in your project directory:
npm install square-connect@latest --save
After upgrading, most API methods (including listLocations) will return Promises, so your original .then() chain should work as expected. Just take a quick look at the latest SDK docs to confirm the method signature hasn’t changed slightly.
2. Wrap the callback-based method in a Promise (if you can’t upgrade)
If you need to stick with your current SDK version, you can convert the callback-style method to a Promise using Node’s built-in util.promisify (which is supported in Node v8+, so your v9.4.0 is good to go):
const util = require('util'); const squareConnect = require('square-connect'); // Initialize your API client as usual const defaultClient = squareConnect.ApiClient.instance; const oauth2 = defaultClient.authentications['oauth2']; oauth2.accessToken = 'YOUR_ACCESS_TOKEN'; const apiInstance = new squareConnect.LocationsApi(); // Convert the callback method to a Promise (don't forget to bind the context!) const listLocationsAsync = util.promisify(apiInstance.listLocations.bind(apiInstance)); // Now use .then() and .catch() like you wanted listLocationsAsync() .then(response => { console.log('Fetched locations:', response); }) .catch(error => { console.error('Error fetching locations:', error); });
The .bind(apiInstance) is crucial here—it makes sure the method has the correct this context when promisified.
3. Double-check your API client setup
It’s worth verifying you’re initializing the LocationsApi instance correctly. A common mistake is forgetting to set up the OAuth2 access token properly, which can lead to unexpected behavior (even if it’s not the direct cause of this error, it’s good to rule out).
Quick side note
Node v9.4.0 is a bit outdated, but it should work fine with either of the fixes above. If you run into other compatibility issues down the line, consider upgrading to a more recent LTS version of Node for better support with modern packages.
内容的提问来源于stack exchange,提问作者Todd Hoff

