Skip to main content

Using Javascript SDK in WeChat Mini Programs

When you use Javascript SDK in WeChat Mini Programs, you will usually encounter an error of adapter is not a function:

Error occurred in the request: {success: false, code: 400, message: "adapter is not a function"}

This is because JS SDK uses axios as the network request library. In WeChat Mini Programs's environment, requests need to use wx.request, so SDK cannot be used directly in WeChat Mini Programs.

However, axios provides the adapter interface, and axios-miniprogram-adapter is axios's request adapter in Mini Program, which makes SDK work properly.

Install axios-miniprogram-adapter

yarn add axios-miniprogram-adapter

Import Adapter

When initializing the AITable instance, specify the adapter as the imported axios-miniprogram-adapter :

import mpAdapter from 'axios-miniprogram-adapter';
import { APITable } from "apitable";

const apitable = new APITable({
token: 'your_api_token',
adapter: mpAdapter,
});

apitable.datasheet('dstxJZ2xZXJnXScfni').records.query().then(resp => {
console.log(resp.data?.records)
})

Reference Code

The following is the basic code of Mini Program generated using the taro framework. After the program is loaded, you can see the console prints out the recorded data.

import { Component } from 'react'
import { View, Text, Button } from '@tarojs/components'
import AITable from 'apitable';
import Taro from '@tarojs/taro';
import mpAdapter from 'axios-miniprogram-adapter';
import './index.less';


export default class Index extends Component {

componentWillMount() { }

componentDidMount() {
const apitable = new APITable({
token: 'your_api_token',
adapter: mpAdapter,
});
apitable.datasheet('dstxJZ2xZXJnXScxxx').records.query().then(resp => {
console.log(resp.data?.records)
})
}

componentWillUnmount() { }

componentDidShow() { }

componentDidHide() { }
render() {
return (
<View className='index'>
<Text>Hello AITable!</Text>
</View>
)
}
}

How do I upload the attachment?

The FormData object is not available because of WeChat Mini Programs's restrictions.In order to ensure that SDK is not coupled with non-standard platforms, uploading attachments in Mini Program is not supported.

You can refer to the solutions from the community and encapsulate the request implementation by yourself.