LoginSignup
5
3

More than 5 years have passed since last update.

NeDBとvue.jsの連携(Nedbのfindをvue.jsのdataで受け取る方法)

Posted at

NeDBのfindメソッドの結果をvue.jsに送るのに苦労したのでメモ。

ポイントは
- NeDBのfindメソッドで変数を使うためにRegExpを利用する必要があること。
- NeDBのfindメソッドの結果を受け取るためにコールバックにbind(this)を設定すること。 ※ここに到達するまで丸2日試行錯誤しました。。

test.json
{"id":"hogehogehoge","name":"ryo","age":"18"}
{"id":"hogehogedoge","name":"yu","age":"26"}
{"id":"hogehogehoge","name":"taro","age":"27"}
test.vue
<template>
        <input v-model="word"></input>
        <button v-on:click="search">検索</button>
        <table>
            <tr v-for="result">
                <td>{{ result.name }}</td>
                <td>{{ result.age }}</td>
            </tr>
        </table>
</template>


<script>
//データベース設定
var Database = require("nedb");
var testDB = new Database({
    filename: './test.json',
    autoload: true
});

//vue スクリプト
export default {
    data () {
        return {
            Word : '',
            risult: '',
        }
    },
    methods: {
        search: function () {
            testDB.find({case: new RegExp(".*"+this.Word+".*", "i")} , function (err, doc) {
                this.risult = doc
            }.bind(this));
        }
    },
    name: 'disease'
}
</script>
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3