var express = require('express');
var app = express();
var sql = require("mssql");
I declare the above library in React Native when running the error:
Android Bundling failed 3690ms (D:\demo\node_modules\expo\AppEntry.js)
The package at "node_modules\mssql\lib\base\connection-pool.js" attempted to import the Node standard library module "node:events".
It failed because the native React runtime does not include the Node standard library.
Dong Lam TrienPosted Apr 10, 2024, 9:02 AM
I ran the code you sent and got the error:
Mukesh NailwalPosted Apr 9, 2024, 6:24 AM
Yes, of course! This is a basic example of a React Native application that reads data from a SQL Server database and displays it in a ListView component.
Get your SQL Server configured and make sure you have a database with some data in it first. Then, install all the necessary dependencies. Here, I will connect to SQL Server for this demo using the mssql package.
npm install mssql
Here's the React Native code:
import React, { useState, useEffect } from 'react';
import { View, Text, FlatList } from 'react-native';
import * as mssql from 'mssql';
const config = {
user: 'mukeshnailwal',
password: 'nailwalmukesh',
server: 'helloMukesh',
database: 'helloNailwal',
};
const App = () => {
const [data, setData] = useState([]);
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
try {
await mssql.connect(config);
const result = await mssql.query('SELECT * FROM nailTable');
setData(result.recordset);
} catch (error) {
console.log('Error:', error);
} finally {
await mssql.close();
}
};
const renderItem = ({ item }) => (
{item.columnName}
);
return (
data={data}
renderItem={renderItem}
keyExtractor={(item, index) => index.toString()}
/>
);
};
export default App;
Here, replace 'mukeshnailwal', 'nailwalmukesh', 'helloMukesh', 'helloNailwal', and 'nailTable' with your actual SQL Server credentials and table name.
As soon as the component mounts, this code establishes a connection to your SQL Server database, retrieves data from the specified table, and displays it in a FlatList component.
I hope this will help you!
Thanks
Dong Lam TrienPosted Apr 9, 2024, 2:19 AM
Hi Mukesh Nailwal !
Do you have a React Native demo that connects sql server to read data and upload to listview ? or are the examples of the library you describe readable by sql server ? If so, can you share the code demo with me ?
Mukesh NailwalPosted Apr 8, 2024, 9:29 AM
No, React Native does not support running Node.js code directly. On mobile devices, React Native executes JavaScript code without access to the Node.js runtime or its APIs.
The error message that you're encountering shows that React Native does not support the module (node:events) that the library you're trying to use (mssql) is trying to import from the Node.js standard library.
To fix this issue, you must locate a another React Native-compatible package or method for communicating with SQL databases. You may work with databases using a number of libraries made especially for React Native. Here are some options that you can try:
react-native-sqlite-storage.
react-native-sqlite-2.
react-native-sqlite.
Select the library that most closely matches your needs, then use one of these substitutes for mssql in your React Native app. To make your code compatible with the chosen library's API, you might also need to make a few changes.
Drop me a comment, if still need help!
Thanks