Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反应本机+;MongoDB Stitch:this.client.getServiceClient不是函数_Mongodb_React Native_Mongodb Stitch - Fatal编程技术网

反应本机+;MongoDB Stitch:this.client.getServiceClient不是函数

反应本机+;MongoDB Stitch:this.client.getServiceClient不是函数,mongodb,react-native,mongodb-stitch,Mongodb,React Native,Mongodb Stitch,我正在尝试为我的Stitch应用程序创建到我的MongoDB数据库集合的简单连接。在componentDidMount()方法中,我初始化默认的应用程序客户端并将其存储在组件的变量中。但是当我尝试在之后设置MongoDB远程时,它不起作用,我得到了 类型错误:this.client.getServiceClient不是函数。(在“this.client.getServiceClient(MongoDB.RemoteMongoClient.factory,“MongoDB atlas”)”中,“t

我正在尝试为我的Stitch应用程序创建到我的MongoDB数据库集合的简单连接。在
componentDidMount()
方法中,我初始化默认的应用程序客户端并将其存储在组件的变量中。但是当我尝试在之后设置MongoDB远程时,它不起作用,我得到了

类型错误:this.client.getServiceClient不是函数。(在“this.client.getServiceClient(MongoDB.RemoteMongoClient.factory,“MongoDB atlas”)”中,“this.client.getServiceClient”未定义)

我已经阅读了所有React原生文档,如或,并且看到结构不一样,但我不希望用户登录应用程序(为什么我使用了
AnonymousCredential()
),即使我使用此结构,我也不知道用户登录后该做什么,如何获取数据?由于没有定义远程客户端,因此没有数据库和集合

以下是我的组件:

import React from "react";
import { StyleSheet, View, TextInput, Button, FlatList } from "react-native";
import PlayerItem from "./PlayerItem";
import { Stitch, AnonymousCredential } from "mongodb-stitch-react-native-sdk";
const MongoDB = require("mongodb-stitch-react-native-services-mongodb-remote");

export default class Search extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            players: [],
        };
        this.query = "";
    }

    componentDidMount() {
        this.client = Stitch.initializeDefaultAppClient("kungfuzone-rzksu");
        const mongodb = this.client.getServiceClient(
            MongoDB.RemoteMongoClient.factory,
            "mongodb-atlas"
        );

        this.db = mongodb.db("players");
        this._displayPlayersOnLoad();
    }

    _displayPlayersOnLoad() {
        this.client.auth
            .loginWithCredential(new AnonymousCredential())
            .then(this._displayPlayers)
            .catch(console.error);
    }

    _displayPlayers() {
        this.db
            .collection("kungfuzone")
            .find({}, { limit: 1000 })
            .asArray()
            .then((players) => {
                this.setState({ players: players });
            });
    }

    _updateQuery(text) {
        this.query = text;
    }

    _searchPlayers(query) {
        if (query.length > 0) {
            this.stitchClient.auth
                .loginWithCredential(new AnonymousCredential())
                .then(() => this.setState({ players: db.find({ name: query }).asArray() }))
                .catch(console.error);
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <TextInput
                    style={styles.textinput}
                    onChangeText={(input) => this._updateQuery(input)}
                    placeholder="Player's name"
                />
                <Button title="Search" onPress={() => this._searchPlayers(this.query)} />
                <FlatList
                    data={this.state.players}
                    keyExtractor={(item) => item.id.toString()}
                    renderItem={({ item }) => <PlayerItem player={item} />}
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        marginTop: 20,
    },
    textinput: {
        marginLeft: 5,
        marginRight: 5,
        height: 50,
        borderColor: "#000000",
        borderWidth: 1,
        paddingLeft: 5,
    },
});
从“React”导入React;
从“react native”导入{样式表、视图、文本输入、按钮、平面列表};
从“/PlayerItem”导入PlayerItem;
从“mongodb Stitch react native sdk”导入{Stitch,AnonymousCredential};
const MongoDB=require(“MongoDB本地服务MongoDB远程”);
导出默认类搜索扩展React.Component{
建造师(道具){
超级(道具);
此.state={
玩家:[],
};
this.query=“”;
}
componentDidMount(){
this.client=Stitch.initializedFaultAppClient(“kungfuzone rzksu”);
const mongodb=this.client.getServiceClient(
MongoDB.RemoteMongoClient.factory,
“mongodb地图册”
);
this.db=mongodb.db(“玩家”);
这是。_displayersonload();
}
_DisplayPlayerOnload(){
this.client.auth
.loginWithCredential(新的匿名凭证())
.然后(这个._显示播放器)
.catch(控制台错误);
}
_displayPlayers(){
这个是.db
.收藏(“功夫通”)
.find({},{limit:1000})
.asArray()
.然后((玩家)=>{
this.setState({players:players});
});
}
_updateQuery(文本){
this.query=文本;
}
_搜索播放器(查询){
如果(query.length>0){
this.stitchClient.auth
.loginWithCredential(新的匿名凭证())
.then(()=>this.setState({players:db.find({name:query}).asArray()}))
.catch(控制台错误);
}
}
render(){
返回(
这是.\u updateQuery(输入)}
占位符=“玩家姓名”
/>
这个。_searchPlayers(this.query)}/>
item.id.toString()}
renderItem={({item})=>}
/>
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
玛金托普:20,
},
文本输入:{
边缘左:5,
marginRight:5,
身高:50,
边框颜色:#000000“,
边框宽度:1,
paddingLeft:5,
},
});

有人能帮忙吗?谢谢:)

实际上,从移动应用程序直接连接到远程数据库不是一个好主意。如何使用或创建API与MongoDB通信?

谢谢您的回答。我需要服务器吗?或者我可以直接从React那里拿到吗?不过,我仍然需要连接/验证数据库,对吗?@Maëlle,您使用的是Stitch。它是专门为“无服务器”体系结构构建的。