question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuxt server side store wont pass the right BaseModel Class to the client

See original GitHub issue

Steps to reproduce

Load data to the store server side Check the data on the client

Expected behavior

The client data should be an FeathersVuex Model, as configured

Actual behavior

The data is a simple javascript object, and even trying to make it a model (Ex. new Model(data)) won’t update it in the store.

System configuration

This started happening in 2.3.1

~/plugins/feathers.js

import feathers from '@feathersjs/feathers'
import rest from '@feathersjs/rest-client'
import axios from 'axios'
import socketio from '@feathersjs/socketio-client'
import auth from '@feathersjs/authentication-client'
import io from 'socket.io-client'
import { CookieStorage } from 'cookie-storage'
import { iff, discard } from 'feathers-hooks-common'
import feathersVuex, { initAuth } from 'feathers-vuex'
const apiUrl = process.env.API_URL
let socket
let restClient
if (process.client) {
  socket = io(apiUrl, { transports: ['websocket'] })
} else {
  restClient = rest(apiUrl)
}
const transport = process.client ? socketio(socket) : restClient.axios(axios)
const storage = new CookieStorage()

const feathersClient = feathers()
  .configure(transport)
  .configure(auth({ storage }))
  .hooks({
    before: {
      all: [
        iff(
          context => ['create', 'update', 'patch'].includes(context.method),
          discard('__id', '__isTemp')
        )
      ]
    }
  })

export default feathersClient

// Setting up feathers-vuex
const { makeServicePlugin, makeAuthPlugin, BaseModel, models, FeathersVuex } = feathersVuex(
  feathersClient,
  {
    serverAlias: 'api', // optional for working with multiple APIs (this is the default value)
    idField: '_id', // Must match the id field in your database table/collection
    whitelist: ['$regex', '$options'],
    enableEvents: process.client
  }
)

export { makeAuthPlugin, makeServicePlugin, BaseModel, models, FeathersVuex, initAuth }

~/store/index.js

import { makeAuthPlugin, initAuth } from '~/plugins/feathers'
const auth = makeAuthPlugin({
  userService: 'user',
  state: {
    publicPages: []
  },
  actions: {
    onInitAuth ({ state, dispatch }, payload) {
      if (payload) {
        dispatch('authenticate', { strategy: 'jwt', accessToken: state.accessToken })
          .then((result) => {
            // handle success like a boss
            // console.log('loged in')
          })
          .catch((error) => {
            // handle error like a boss
            console.log(error)
          })
      }
    }
  }
})

const requireModule = require.context(
  // The path where the service modules live
  './services',
  // Whether to look in subfolders
  false,
  // Only include .js files (prevents duplicate imports`)
  /.js$/
)
const servicePlugins = requireModule
  .keys()
  .map(modulePath => requireModule(modulePath).default)

export const modules = {
}

export const state = () => ({
})

export const mutations = {
}

export const actions = {
  nuxtServerInit ({ commit, dispatch }, { req }) {
    return initAuth({
      commit,
      dispatch,
      req,
      moduleName: 'auth',
      cookieName: 'feathers-jwt'
    })
  },
  nuxtClientInit ({ state, dispatch }, context) {
    if (state.auth.accessToken) {
      return dispatch('auth/onInitAuth', state.auth.payload)
    }
  }
}

export const getters = {
  
}

export const plugins = [ ...servicePlugins, auth ]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
guzzcommented, Dec 16, 2019

I think is a good solution, i can make a PR by the end of this week, probably thursday I can put my hands on it.

0reactions
marshallswaincommented, Dec 16, 2019

I haven’t tested the above code, but it should be pretty close to the real deal (minus the TypeScript conversion). I will happily accept a PR if you’d like to put one together. If you don’t have the bandwidth to do it, please let me know and I’ll do it in a few days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Side Rendering - Nuxt
Server -side rendering (SSR), is the ability of an application to contribute by displaying the web-page on the server instead of rendering it...
Read more >
Common Patterns | FeathersVuex
Create your nuxt store with the right nuxt pattern, exporting an Vuex store will be deprecated on nuxt 3. // ~/store/index.js import {...
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
In this article, Toptal Freelance Front-end Engineer Ben Jones introduces us to Nuxt.js, a server-side rendering library for Vue.js, inspired by the popular ......
Read more >
Using FastAPI to Build Python Web APIs - Real Python
You will learn more about those features next. This code defines your application, but it won't run on itself if you call it...
Read more >
Models - Django documentation
first_name and last_name are fields of the model. Each field is specified as a class attribute, and each attribute maps to a database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found