Christian
Christian

Reputation: 779

nativescript-sqlite and angular2 module not found exception

I have a nativescript with angular2 application for android where I want to make use of the nativescript-sqlite of offline storage capabilities. The problem is I am getting the following exception:

Failed to find module: "nativescript-sqlite", relative to: /app/tns_modules/

on the line below:

var Sqlite = require("nativescript-sqlite");

I install the plugin using the following command

tns plugin add nativescript-sqlite

I have created a db.service.ts file with and angular service, the contents below:

import {Injectable} from "@angular/core";
import {Config} from "../config";
import {Observable} from "rxjs/Rx";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";

import {Profile} from "../profile/profile";

var Sqlite = require("nativescript-sqlite");

@Injectable()
export class DbService {
    database: any;

    constructor() {
        (new Sqlite("gtel.db")).then(db => {
            this.database = db;
            db.resultType(Sqlite.RESULTSASOBJECT);
            this.database.execSQL("CREATE TABLE IF NOT EXISTS profile (id INTEGER PRIMARY KEY AUTOINCREMENT," +
                " username TEXT, idnumber TEXT, firstname TEXT, lastname TEXT, mobilenumber TEXT, emailaddress TEXT)").then(id => {
                    console.log("created table profile")
                }, error => {
                    console.log("created table profile error", error);
                });
        }, error => {
            console.log("OPEN DB ERROR", error);
        });

    }

    createProfile(profile: Profile){
        return this.database.execSQL("INSERT INTO profile(username, idnumber, firstname, lastname, mobilenumber, emailaddress) VALUES (?, ?, ?, ?, ?, ?)",
                        [profile.username, profile.idNumber, profile.firstName, profile.lastName, profile.mobileNumber, profile.emailAddress]);
    }

    getProfile(id: number){
        return this.database.get('select * from Hello where id=?', [id])
    }

    handleErrors(error: Response) {
        console.log(JSON.stringify(error.json()));
        return Observable.throw(error);
    }
}

Your help will be greatly appreciated.

Upvotes: 5

Views: 1166

Answers (1)

Tim Hallyburton
Tim Hallyburton

Reputation: 2939

Most of these module errors can be solved by removing the platform(s) and then adding it again.

For Android that would be:

tns platform remove android
tns platform add android

and for iOS:

tns platform remove ios
tns platform add ios

This issue has been solved as you can see in the comments. Just answering this so everyone can see it

Upvotes: 1

Related Questions